Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
72 3 1 5 7 6 41 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2
1 #include2 #include 3 4 typedef struct TNode *Ptr; 5 typedef Ptr Bintree; 6 struct TNode 7 { 8 int data; 9 Bintree left;10 Bintree right;11 };12 int inorder[50]; //中序13 int post[50]; //后序14 void Leverorder( Bintree tree);15 Bintree Create( int in[], int post[],int n);16 int main()17 {18 int n;19 int i;20 Bintree tree;21 scanf("%d",&n);22 for( i=0; i data = post[n-1];44 temp->left = Create( in,post,index);45 temp->right = Create( in+index+1,post+index,n-index-1);46 return temp;47 }48 49 void Leverorder( Bintree tree)50 {51 if( tree== NULL)52 return ;53 Bintree T[50];54 int i=0,j=0;55 int flag=0;56 T[j++] = tree;57 while( i data);64 if( s->left )65 T[j++]=s->left;66 if( s->right)67 T[j++]=s->right;68 i++;69 }70 }