已知树的中序+后序/先序遍历,建立二叉树
来源:互联网 发布:网络规划设计师 编辑:程序博客网 时间:2024/05/22 12:12
已知树的中序和后序遍历,建立二叉树(个人感觉理解不了可以直接硬记,慢慢就能理解,知道思想不能理解也无所谓,会用就行)
#include<cstdio>#include<iostream>#include<algorithm>#include<queue>using namespace std;int n;int hou[50];int zhong[50];typedef struct Btree{ int data; struct Btree* left; struct Btree* right;};Btree* build(int *hou,int *zhong,int len){ if(len<=0) return NULL; Btree* tmp=new Btree; tmp->data=hou[len-1]; int num=0; for(int i=0;i<len;i++) { if(zhong[i]==hou[len-1]) { num=i; break; } } int index=num; tmp->left=build(hou,zhong,index); tmp->right=build(hou+index,zhong+index+1,len-index-1); return tmp;}int ans[500];int cur;void print(Btree *root){ queue<Btree*> Q; Q.push(root); Btree *tmp=Q.front(); cout<<tmp->data; if(tmp->left!=NULL) Q.push(tmp->left); if(tmp->right!=NULL) Q.push(tmp->right); Q.pop(); while(!Q.empty()) { Btree *tmp=Q.front(); Q.pop(); cout<<" "<<tmp->data; //ans[cur++]=tmp->data; if(tmp->left!=NULL) { // cout<<tmp->data; Q.push(tmp->left); } if(tmp->right!=NULL) { Q.push(tmp->right); } }}int main(){ scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&hou[i]); } for(int i=0;i<n;i++) { scanf("%d",&zhong[i]); } Btree *root=build(hou,zhong,n); //print(root); return 0;}
已知树的中序和先序遍历,建立二叉树
*#include<cstdio>#include<iostream>#include<algorithm>#include<queue>using namespace std;int n;int xian[50];int zhong[50];typedef struct Btree{ int data; struct Btree* left; struct Btree* right;};//typedef struct Btree Btree;Btree* build(int *xian,int *zhong,int len){ if(len<=0) return NULL; Btree* tmp=new Btree; tmp->data=xian[0]; int num=0; for(int i=0;i<len;i++) { if(zhong[i]==xian[0]) { num=i; break; } } int index=num; tmp->left=build(xian+1,zhong,index); tmp->right=build(xian+index+1,zhong+index+1,len-index-1); return tmp;}void print(Btree *root){ queue<Btree*> Q; Q.push(root); Btree *tmp=Q.front(); cout<<tmp->data; if(tmp->right!=NULL) Q.push(tmp->right); if(tmp->left!=NULL) Q.push(tmp->left); Q.pop(); while(!Q.empty()) { Btree *tmp=Q.front(); Q.pop(); cout<<" "<<tmp->data; //ans[cur++]=tmp->data; if(tmp->right!=NULL) { Q.push(tmp->right); } if(tmp->left!=NULL) { // cout<<tmp->data; Q.push(tmp->left); } }}int main(){ scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&zhong[i]); } for(int i=0;i<n;i++) { scanf("%d",&xian[i]); } Btree *root=build(xian,zhong,n); //print(root); return 0;}
0 0
- 已知树的中序+后序/先序遍历,建立二叉树
- 已知二叉树的先序、中序遍历序列,求其后序遍历结果。(hduoj1710)
- 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历
- 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历
- 二叉树已知先序遍历和中序遍历得到后序遍历
- 建立二叉树(已知中序遍历和先序遍历)
- 建立二叉树,先、中、后序遍历
- 已知二叉树的先序遍历和中序遍历序列,输出后序遍历序列
- 已知满二叉树的先序遍历,求其后序遍历
- 已知二叉树的先序和中序遍历,求后序遍历及二叉树
- 已知二叉树的先序遍历和中序遍历,如何求后序遍历
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 建立二叉树,实现二叉树的先序遍历、中序和后序遍历的非递归算法
- 二叉树的建立和中序、先序、后序遍历
- 二叉树的建立以及先序,中序,后序遍历
- 已知二叉树的先序和中序求后序遍历
- 已知二叉树的先序和中序求后序遍历
- Hibernate中Criteria的完整用法
- Windows NT 下Service 的 编 程
- iOS 创建pod私有库
- 欢迎使用CSDN-markdown编辑器
- 全新部署tomcat Web项目到服务器的方法
- 已知树的中序+后序/先序遍历,建立二叉树
- 炉石传说全套美术资源(卡牌)+UI 108M
- SpringMVC整合Mongodb开发,高级操作
- Swift 3.1 官方文档翻译(一)——关于 Swift
- Swift3.0 block 传值
- 排序算法——堆排序
- GUI_修改窗体的图标代码
- Android中去掉标题栏的3种方法
- xib 控件设置圆角 label button view 等