Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11643 Accepted Submission(s): 5186
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.
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
Source
HDU 2007-Spring Programming Contest
#include<bits/stdc++.h>
using namespace std;
int pre[1010],mid[1010];
struct Tree{
int data;
Tree *left;
Tree *right;
}*root;
Tree * create(int *a,int *b,int n)
{
Tree *t;
for(int i=0;i<n;i++){
if(a[0]==b[i]){
t=(Tree*)malloc(sizeof(Tree));
t->data =b[i];
t->left =create(a+1,b,i);
t->right =create(a+i+1,b+i+1,n-i-1);
return t;
}
}
return NULL;
}
void Last(Tree *kk){
if(kk!=NULL){
Last(kk->left);
Last(kk->right);
if(kk==root) printf("%d\n",kk->data);
else printf("%d ",kk->data);
}
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
root=NULL;
for(int i=0;i<n;i++)
{
scanf("%d",&pre[i]);
}
for(int i=0;i<n;i++)
{
scanf("%d",&mid[i]);
}
root=create(pre,mid,n);
Last(root);
}
}
https://www.cnblogs.com/weiyi-mgh/p/6616008.html
二叉树前序中序后序的推算
后续和中序 推前序
6
1 3 4 2 8 6
1 2 3 4 6 8
#include<bits/stdc++.h>
using namespace std;
int last[1010],mid[1010];
struct node{
node* LL;
node* RR;
int data;
}*root;
int n,k;
node * build(int*a,int *b,int nn){
node *t;
for(int i=0;i<nn;i++){
if(a[nn-1]==b[i]){
t=(node*)malloc(sizeof(node));
t->data=a[nn-1];
t->LL=build(a,b,i);
t->RR=build(a+i,b+1+i,nn-i-1);
return t;
}
}
return NULL;
}
void pre( node *gg){
if(gg!=NULL){
++k;
if(k!=n) {
cout<<gg->data<<" ";
}else {
cout<<gg->data <<endl;
}
pre(gg->LL);
pre(gg->RR);
}
}
int main(){
while(~scanf("%d",&n)){
root=NULL;
for(int i=0;i<n;i++){
scanf("%d",&last[i]);
}
for(int i=0;i<n;i++){
scanf("%d",&mid[i]);
}
root=build(last,mid,n);
k=0;
pre(root);
}
}
6
1 3 4 2 8 6
1 2 3 4 6 8
ans=6 2 1 4 3 8