PAT Grade --A1119 Pre- and Post-order Traversals [30]

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

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 preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. 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 1:

7
1 2 3 4 6 7 5
2 6 7 4 5 3 1

Sample Output 1:

Yes
2 1 6 4 7 3 5

Sample Input 2:

4
1 2 3 4
2 4 3 1

Sample Output 2:

No
2 1 3 4


. 1 #include <the iostream>
 2 #include <Vector>
 . 3  the using  namespace STD;
 . 4  int n-, A;
 . 5 Vector < int > preOrder, postorder, inOrder;
 . 6  BOOL In Flag = to true ; // represents the tree is not unique morphology 
. 7  void getInOrder ( int the root, int left, int right)
 . 8  {
 . 9      IF (left> = right)
 10      {
 . 11          IF (left == right) // only one node
12 is              inOrder.push_back (preOrder [the root]);
 13 is          return ;
 14      }
 15      int I = left;
 16      the while (I <right && preOrder [+ the root . 1 !] = Postorder [I]) // Find next preorder traversal a node in the sequence of positions 
. 17          ++ I;
 18 is      IF (I == right - . 1 ) // first root of the next node in the sequence of the root node in the root sequence exactly equal. 1-right 
. 19          in Flag = to false ;
 20 is      getInOrder (the root + . 1 , left, I);
 21 is      inOrder.push_back (preOrder [the root]);
 22 is     getInOrder(root + i - left + 2, i + 1, right - 1);
23 }
24 int main()
25 {
26     cin >> n;
27     for (int i = 0; i < n; ++i)
28     {
29         cin >> a;
30         preOrder.push_back(a);
31     }
32     for (int i = 0; i < n; ++i)
33     {
34         cin >> a;
35         postOrder.push_back(a);
36     }
37     getInOrder(0, 0, n - 1);
38     cout << (flag ? "Yes" : "No") << endl;
39     for (int i = 0; i < n; ++i)
40         cout << (i > 0 ? " " : "") << inOrder[i];
41     cout << endl;
42     return 0;
43 }

 

Guess you like

Origin www.cnblogs.com/zzw1024/p/11462895.html