PAT Grade --A1020 Tree Traversals

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 (≤), 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:

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

Sample Output:

4 1 6 3 5 7 2


Known preorder traversal order and output layer preorder

. 1 #include <the iostream>
 2 #include <Queue>
 . 3  the using  namespace STD;
 . 4  int * POS, the ord *; // after storage and the sequence preorder data 
. 5  struct the Node
 . 6  {
 . 7      int Val;
 . 8      the Node L *, * R & lt;
 . 9      the Node ( int A = 0 ): Val (A), L (nullptr a), R & lt (nullptr a) {}
 10  };
 . 11 the Node * createTree ( int POSL, int posR, int ordL, int ORDR)
 12 is  {
 13 is     IF (POSL> posR)
 14          return nullptr a;
 15      the Node * = the root new new the Node ();
 16      directory root-> Val POS = [posR]; // root values 
. 17      int K;
 18 is      for (K = ordL; K <= ORDR; ++ K)
 . 19      {
 20 is          IF (the ord [K] == POS [posR]) // root of the tree to find the original 
21 is              BREAK ;
 22 is      }
 23 is      int numL = K - ordL; // number of left sub-tree nodes
 24      // recursive construction left subtree 
25      directory root-> L = createTree (POSL, POSL + numL -. 1 , ordL, K - . 1 );
 26 is      // recursive construction right subtree 
27      directory root-> = R & lt createTree (POSL + numL, posR - . 1 , K + . 1 , ORDR); // remove the root 
28      return the root;
 29  }
 30  void getResBFS (the Node * the root)
 31 is  {
 32      Queue <the Node *> Q;
 33 is      the Node * P = nullptr a;
 34 is      q.push (the root);
 35      COUT << directory root-> Val;
 36      the while (! q.empty ())
 37      {
 38         p = q.front();
39         if (p != root)
40             cout << " " << p->val;
41         q.pop();
42         if (p->l != nullptr)
43             q.push(p->l);
44         if (p->r != nullptr)
45             q.push(p->r);
46     }
47     cout << endl;
48 }
49 
50 int main()
51 {
52     int N;
53     cin >> N;
54     pos = new int[N];
55     ord = new int[N];
56     for (int i = 0; i < N; ++i)
57         cin >> pos[i];
58     for (int i = 0; i < N; ++i)
59         cin >> ord[i];
60     Node* root = createTree(0, N - 1, 0, N - 1);
61     getResBFS(root);
62      return  0 ;    
63 }

 

Guess you like

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