Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4330 Accepted Submission(s): 1970
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.
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
#include<stdio.h>
#include<string.h>
int str1[],str2[],str3[];
int sum;
int getlen(int root,int str2[]){
int cnt=;
int i=;
while(){
if(str2[i]==root)
return cnt;
i++;
cnt++; }
}
void change(int m,int str1[],int str2[]){
if(m==)
return ;
int len=getlen(str1[],str2);
change(len,str1+,str2);
change(m--len,str1++len,str2+len+);
str3[++sum]=str1[];
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
sum=-;
memset(str1,,sizeof(str1));
memset(str2,,sizeof(str2));
memset(str3,,sizeof(str3));
for(int i=;i<n;i++)
scanf("%d",&str1[i]);
for(int i=;i<n;i++)
scanf("%d",&str2[i]);
change(n,str1,str2);
for(int i=;i<n;i++)
printf("%d%c",str3[i],i==n-?'\n':' ');
}
return ;
}