hdu1710 二叉树(C/C++)
hdu1710
题目地址:https://acm.dingbacode.com/showproblem.php?pid=1710
(最近几天杭电原网址开不进去了,之后应该可以通。。吧)
Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
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.
![]()
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.

Input
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.
Output
For each test case print a single line specifying the corresponding postorder sequence.
Sample Input
9 1 2 4 7 3 5 8 9 6 4 7 2 1 8 5 9 3 6
Sample Output
7 4 2 8 9 5 6 3 1
题目大意
输入二叉树的先序和中序遍历序列,求后序遍历
输入样例
先序:1 2 4 7 3 5 8 9 6
中序:4 7 2 1 8 5 9 3 6
输出样例
后序:7 4 2 8 9 5 6 3 1
AC代码
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=1010; 4 int pre[N],in[N],post[N]; 5 int k; 6 struct node{ 7 int value; 8 node *l,*r; 9 node(int value = 0, node *l=NULL,node *r=NULL):value(value),l(l),r(r){} 10 }; 11 12 void buildtree(int l,int r,int &t,node *&root){ //建树,一定是*&root!!!!!!!!!!!! 13 int flag=-1; 14 for(int i=l;i<=r;i++){ //字母l-r,误敲成数字1也A了。。 15 if(in[i]==pre[t]){ //找到先序中的根在中序里的位置 16 flag=i; //存在flag中 17 break; 18 } 19 } 20 if(flag==-1) return; //结束 21 root=new node(in[flag]); //新建结点 22 t++; 23 if(flag>l) buildtree(l,flag-1,t,root->l); 24 if(flag<r) buildtree(flag+1,r,t,root->r); 25 } 26 27 void preorder(node *root){ //先序遍历 28 if(root!=NULL){ 29 post[k++]=root->value; //输出 30 preorder(root->l); 31 preorder(root->r); 32 } 33 } 34 35 void inorder(node *root){ //中序遍历 36 if(root!=NULL){ 37 inorder(root->l); 38 post[k++]=root->value; 39 inorder(root->r); 40 } 41 } 42 43 void postorder(node *root){ //后序遍历 44 if(root!=NULL){ 45 postorder(root->l); 46 postorder(root->r); 47 post[k++]=root->value; 48 } 49 } 50 51 void remove_tree(node *root){ //释放空间 52 if(root==NULL) return; 53 remove_tree(root->l); 54 remove_tree(root->r); 55 delete root; 56 } 57 58 int main(){ 59 int n; 60 while(~scanf("%d",&n)){ 61 for(int i=1;i<=n;i++) cin>>pre[i]; 62 for(int i=1;i<=n;i++) cin>>in[i]; 63 node *root; 64 int t=1; 65 buildtree(1,n,t,root); //1到n,是数字1 66 k=0; //记录结点个数 67 postorder(root); 68 for(int i=0;i<k;i++){ 69 printf("%d%c",post[i],i==k-1?'\n':' '); 70 } 71 remove_tree(root); 72 } 73 return 0; 74 }

浙公网安备 33010602011771号