This problem requires preorder traversal results based on the given sequence and a binary output preorder traversal of the tree results.
Input formats:
The first line gives a positive integer N ( ≤ . 3 0), is the number of nodes in the tree. Subsequently two rows, each row gives N integers, preorder traversal order and respectively correspond to the results, separated by spaces between numbers. Enter the correct title to ensure that the corresponding binary tree.
Output formats:
Output line
Preorder:
and the first-order tree traversal results. There is a space between numbers, end of the line may not have the extra space.Sample input:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7Sample output:
Preorder: 4 1 3 2 6 5 7
1 // index from 0 2 void preOrder ( int POST [], int in [], int len) . 3 { . 4 IF (len <= 0 ) return ; . 5 . 6 // find the parent node . 7 int I = 0 ; . 8 the while (POST [len . 1 ] =! in [I]) I ++ ; . 9 10 the printf ( " % D " , POST [len . 1 ]); . 11 12 is Preorder(post,in,i); 13 Preprder(post+i,in+i+1,len-i-1); 14 }