PAT_A1119 Pre- and Post-order Traversals

Source:

PAT A1119 Pre- and Post-order Traversals (30 分)

Description:

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

Keys:

Attention:

  • 如果树中所有的分支结点都有两个孩子,那么由先序和后序可以唯一确定一棵二叉树;

Code:

 1 /*
 2 Data: 2019-06-29 17:43:37
 3 Problem: PAT_A1119 Pre- and Post-order Traversals
 4 AC: 36:40
 5 
 6 题目大意:
 7 给出先序和后序遍历,输出任意中序遍历,并判断是否唯一
 8 
 9 基本思路:
10 即由先序和后序是否可以唯一的构造一棵二叉树?
11 易知先序+中序,后序+中序,层序+中序,可以唯一的构造一棵二叉树,
12 为什么呢?因为已知根结点和中序遍历,能够求出根结点的左子树和右子树,进而递归的构造一棵二叉树
13 重点在于找到根结点的左子树和右子树,
14 先序遍历中,根结点root的下一个结点设为左子树的根结点lchild
15 后序遍历中,找到lchild,那么lchild之前(含root)的结点均为左子树,lchild之后直至root之前的结点均为右子树
16 这样,我们可以获得一棵二叉树;
17 如何判定是否唯一呢?
18 若分支结点既有左子树,又有右子树,那么我们可以通过上述方法唯一的构造一棵二叉树
19 若分支结点只有左子树或右子树
20 则我们既可以认为它是左子树,又可以认为它是右子树,这样就产生了多个解, 即此时二叉树不唯一
21 当我们总假设该子树为左子树,这样可以就获得其中一棵二叉树了
22 */
23 #include<cstdio>
24 #include<vector>
25 using namespace std;
26 const int M=35;
27 int pre[M],post[M],ans=1;
28 vector<int> in;
29 
30 void Travel(int preL, int preR, int postL, int postR)
31 {
32     if(preL > preR){
33         ans=0;
34         return;
35     }
36     if(preL == preR){
37         in.push_back(pre[preL]);
38         return;
39     }
40     int k;
41     for(k=postL; k<=postR; k++)
42         if(pre[preL+1] == post[k])
43             break;
44     int numLeft = k-postL;
45     Travel(preL+1,preL+1+numLeft,postL,k);
46     in.push_back(pre[preL]);
47     Travel(preL+1+numLeft+1,preR,k+1,postR-1);
48 }
49 
50 int main()
51 {
52 #ifdef ONLINE_JUDGE
53 #else
54     freopen("Test.txt", "r", stdin);
55 #endif // ONLINE_JUDGE
56 
57     int n;
58     scanf("%d", &n);
59     for(int i=0; i<n; i++)
60         scanf("%d", &pre[i]);
61     for(int i=0; i<n; i++)
62         scanf("%d", &post[i]);
63     Travel(0,n-1,0,n-1);
64     if(ans) printf("Yes\n");
65     else    printf("No\n");
66     for(int i=0; i<n; i++)
67         printf("%d%c", in[i],i==n-1?'\n':' ');
68 
69     return 0;
70 }

 

posted @ 2019-06-10 21:48  林東雨  阅读(254)  评论(0编辑  收藏  举报