HDU 1710二叉树的前序和中序遍历求后序遍历(结构体+指针)
2016-05-14 23:59
423 查看
Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5238 Accepted Submission(s): 2417
[align=left]Problem Description[/align]
A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can
be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.
In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.
In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.
In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.
Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.
[align=left]Input[/align]
The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and
inorder sequence. You can assume they are always correspond to a exclusive binary tree.
[align=left]Output[/align]
For each test case print a single line specifying the corresponding postorder sequence.
[align=left]Sample Input[/align]
9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
[align=left]Sample Output[/align]
7 4 2 8 9 5 6 3 1
题目要求:给你有n个结点的二叉树,接下来给你这个二叉树的前序遍历和中序遍历,让你输出他的后序遍历
思路很清晰:递归的套路嘛,
前序的第一个数x为当前根,在中序中找到这个数x,
中序x的左边就是左子树的中序遍历并统计数目为w1个,中序x右面就是右子树的中序遍历并统计数目为w2个。
前序除了第一个结点之后的w1个为左子树前序遍历,接下来的w2个为右子树前序遍历
然后先建树,然后后序遍历一遍,指针用的不6,写了好久。
#include<cstring> #include<iostream> #include<cstdio> #include<conio.h> using 4000 namespace std; const int maxn = 1005 ; struct node { int date; node *l; node *r; node(){ date = 0; l=NULL; r=NULL; } }; void create(node *t,int str1[],int len1,int str2[],int len2) { if(len1==0) { t = NULL ; return ; } int index = 1; for(int i=1; i<=len2; i++) { if(str2[i]==str1[1]) { index = i; break ; } } int ltemp[maxn]; int ltemplen = index-1; int rtemp[maxn]; int rtemplen = len2-index; for(int i=1; i<=ltemplen;i++)ltemp[i]=str2[i]; int c=1; for(int i=index+1; i<=len2;i++)rtemp[c++]=str2[i]; int temp1[maxn]; int temp2[maxn]; c=1; for(int i=2; i<=ltemplen+1;i++)temp1[c++] = str1[i]; c=1; for(int i=ltemplen+2; i<=len1;i++)temp2[c++] = str1[i]; t->date = str1[1]; if(index-1!=0){ t->l=new node(); create(t->l,temp1,index-1,ltemp,index-1); } if(len2-index!=0){ t->r=new node(); create(t->r,temp2,len2-index,rtemp,len2-index); } return ; } int w=0; int ans[maxn] ; void lastorder(node *t) { if(t!=NULL){ lastorder(t->l); lastorder(t->r); ans[w++]=t->date; } } int main() { int str1[maxn] ; int str2[maxn] ; int n ; //freopen("in.txt","r",stdin); while(~scanf("%d",&n)) { w=0; node *root = new node(); for(int i=1; i<=n; i++) { scanf("%d",&str1[i]); } for(int i=1; i<=n; i++) { scanf("%d",&str2[i]); } int len1=n; int len2=n; create(root,str1,len1,str2,len2); lastorder(root); for(int i=0;i<w-1;i++){ printf("%d ",ans[i]); }printf("%d\n",ans[w-1]); } return 0; }
相关文章推荐
- HTML5列表、布局和块
- U-Boot移植——链接地址、运行地址、加载地址、存储地址
- nginx地址跳转及域名解析
- 78. Subsets
- (二)JAVA使用POI操作excel
- 杭电 2047 递推
- iOS图形绘制UIBezierPath篇
- odroid Xu4介绍
- 我想见证自己的进步
- 程序员常用网站汇总
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
- sqoop学习笔记
- 一个操作系统的实现(2)-认识保护模式
- 排序算法——直接插入排序
- Android volley 解析(四)之缓存篇
- Android Studio 2.0下的虚拟机创建
- ScrollView的学习
- greenrobot的EventBus 源码解析
- SIFT特征提取分析
- Android volley 解析(三)之文件上传篇