给出 中序&后序 序列 建树;给出 先序&中序 序列 建树
已知 中序&后序 建立二叉树:
SDUT 1489
Description
Input
Output
Sample Input
2
dbgeafc
dgebfca
lnixu
linux
Sample Output
abdegcf
xnliu 代码实现:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include<string.h>
#include<stack>
#include<set>
#include <queue>
using namespace std;
struct Tree
{
char a;
Tree * l;
Tree *r;
};
//已知中序与后序递归建树::
Tree * ipCreatTree(char *instar,char *inend ,char *poststar,char*postend) //instar 中序首地址 inend 中序尾地址 poststar后序首地址 postend后序尾地址
{
Tree *root = (Tree*)malloc(sizeof(Tree)); root->a = *postend; root->l = NULL; root->r = NULL; if(instar==inend&&poststar==postend) return root; char *inp = instar; while((*inp != root->a)&&(inend-inp>=)) ++inp; int leftLength = inp - instar; char *leftInorderEnd = instar+leftLength-; if(leftLength>)
root->l = ipCreatTree(instar,leftInorderEnd,poststar,poststar+leftLength-);
if(leftLength<inend-instar)
root->r = ipCreatTree(leftInorderEnd+,inend,poststar+leftLength,postend-); return root;
}
void Preorder(Tree *r)
{
if(r == NULL)
return;
else
{
printf("%c",r->a);
Preorder(r->l);
Preorder(r->r);
}
}
int main()
{
int t;
char inorder[];
char postorder[];
Tree *root;
scanf("%d",&t);
getchar();
while(t--)
{
scanf("%s%s",inorder,postorder);
int inlen = strlen(inorder);
int postlen = strlen(postorder);
root=ipCreatTree(inorder,inorder+inlen-,postorder,postorder+postlen-);
Preorder(root);
printf("\n");
}
return ;
}
已知 先序&中序 建立二叉树:
SDUT 3343
Description
给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。
Input
Output
Sample Input
9
ABDFGHIEC
FDHGIBEAC
Sample Output
5
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include<string.h>
#include<stack>
#include<set>
#include <queue>
using namespace std;
struct Tree
{
char a;
Tree * l;
Tree *r;
};
//已知先序与中序递归建树::
Tree * piCreatTree(char *instar,char *inend ,char *prestar,char*preend) //instar 中序首地址 inend 中序尾地址 poststar先序首地址 postend先序尾地址
{
Tree *root = (Tree*)malloc(sizeof(Tree)); root->a = *prestar; root->l = NULL; root->r = NULL; if(instar==inend&&prestar==preend) return root; char *inp = instar; while((*inp != root->a)&&(inend-inp>=)) ++inp; int leftLength = inp - instar; char *leftInorderEnd = instar+leftLength-; if(leftLength>)
root->l = piCreatTree(instar,leftInorderEnd,prestar+,prestar+leftLength);
if(leftLength<inend-instar)
root->r = piCreatTree(leftInorderEnd+,inend,prestar+leftLength+,preend); return root;
} int Depth(Tree *r)
{
int hl,hr;
if(r==NULL) return ;
hl = Depth(r->l);
hr = Depth(r->r);
if(hl>=hr) return hl+;
else return hr+;
}
int main()
{
int t;
char preorder[];
char inorder[];
Tree *root;
while(~scanf("%d",&t))
{
getchar();
scanf("%s%s",preorder,inorder);
int inlen = strlen(inorder);
int prelen = strlen(preorder);
root=piCreatTree(inorder,inorder+inlen-,preorder,preorder+prelen-);
printf("%d\n",Depth(root));
}
return ;
}
本文为个人随笔,如有不当之处,望各位大佬多多指教.
若能为各位博友提供小小帮助,不胜荣幸.
给出 中序&后序 序列 建树;给出 先序&中序 序列 建树的更多相关文章
- 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序
接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...
- LeetCode:二叉树的前、中、后序遍历
描述: ------------------------------------------------------- 前序遍历: Given a binary tree, return the pr ...
- 前、中、后序遍历随意两种是否能确定一个二叉树?理由? && 栈和队列的特点和区别
前序和后序不能确定二叉树理由:前序和后序在本质上都是将父节点与子结点进行分离,但并没有指明左子树和右子树的能力,因此得到这两个序列只能明确父子关系,而不能确定一个二叉树. 由二叉树的中序和前序遍历序列 ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT 1489 求二叉树的先序遍历 (中序后序还原二叉树)
求二叉树的先序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description 已知一 ...
- SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...
- 二叉树 遍历 先序 中序 后序 深度 广度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 前序+中序->后序 中序+后序->前序
前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...
- C中二叉排序树的非递归和递归插入操作以及中序遍历代码实现【可运行】
C中二叉排序树的非递归和递归插入操作以及中序遍历代码实现[可运行] #include <stdio.h> #include <stdlib.h> typedef int Key ...
随机推荐
- linux 命令——6 rmdir(转)
今天学习一下linux中命令: rmdir命令.rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的.(注意,rm - r dir命令可代替rmdir,但是有很大危险性.)删 ...
- IOS 旋转+缩放(手势识别)
@interface NJViewController ()<UIGestureRecognizerDelegate> @property (weak, nonatomic) IBOutl ...
- 运维自动化之Cobbler系统安装详解
原文链接 参考文档 参考文档SA们现在都知道运维自动化的重要性,尤其是对于在服务器数量按几百台.几千台增加的公司而言,单单是装系统,如果不通过自动化来完成,根本是不可想象的. 运维自动化安装方面,早期 ...
- vue项目跨域问题
跨域 了解同源政策:所谓"同源"指的是"三个相同". 协议相同 域名相同 端口相同 解决跨域 jsonp 缺点:只能get请求 ,需要修改B网站的代码 cors ...
- git与github账号建立SSH连接
第1步:创建SSH Key.在用户主目录下,(就是在你的工作空间一层)看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步 ...
- Java poi 导出Excel并下载到客户端
Maven配置,包含了其他文件格式的依赖,就全贴出来了 <dependency> <groupId>org.apache.poi</groupId> <art ...
- NopCommerce(Core)学习目录
1.NopCommerce下载运行 2.登录及权限 3.日志 4.依赖注入使用autofac 5.插件实现 ...
- js数据结构与算法--递归
递归,函数自己调用自己 return 返回值, 后面的代码不执行 function fn(num){ console.log(num) if(num == 0){ return; } fn(num-1 ...
- PhotoSwipe图片展示插件
这个插件相当棒!功能也很强大,可以自行体会. 官方网址:http://www.photoswipe.com/ github地址:https://github.com/codecomputerlove/ ...
- JsRender (js模板引擎)
最近学习了一下Jsrender模板渲染工具,非常不错,功能比较强大,官网说他是“简单直观 功能强大 可扩展的 快如闪电”确实如此.总结一下!! jsRender 三个最重要的概念:模板.容器和数据. ...