A1119. Pre- and Post-order Traversals

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<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<vector>
 5 using namespace std;
 6 int pre[31], post[31], N;
 7 typedef struct NODE{
 8     struct NODE* lchild, *rchild;
 9     int data;
10 }node;
11 int exam(int preL, int preR, int postL, int postR){
12     if(preL > preR && postL > postR)
13         return 1;
14     if(pre[preL] != post[postR])
15         return 0;
16     int len = preR - preL;
17     int ans = 0;
18     for(int i = 0; i <= len; i++){
19         ans += exam(preL + 1, preL + i, postL, postL - 1 + i) * exam(preL + i + 1, preR, postL + i, postR - 1);
20     }
21     return ans;
22 }
23 int create(int preL, int preR, int postL, int postR, node* &root){
24     if(preL > preR && postL > postR){
25         root = NULL;
26         return 1;
27     }
28     if(pre[preL] == post[postR]){
29         root = new node;
30         root->data = pre[preL];
31         root->lchild = NULL;
32         root->rchild = NULL;
33     }else{
34         return 0;
35     }
36     int ans = 0;
37     int len = preR - preL;
38     for(int i = 0; i <= len; i++){
39         ans = create(preL + 1, preL + i, postL, postL - 1 + i, root->lchild) && create(preL + i + 1, preR, postL + i, postR - 1, root->rchild);
40         if(ans != 0)
41             return 1;
42     }
43     return ans;
44 }
45 vector<int> visit;
46 void preOrder(node* root){
47     if(root == NULL)
48         return;
49     preOrder(root->lchild);
50     visit.push_back(root->data);
51     preOrder(root->rchild);
52 }
53 
54 int main(){
55     scanf("%d", &N);
56     for(int i = 1; i <= N; i++){
57         scanf("%d", &pre[i]);
58     }
59     for(int i = 1; i <= N; i++){
60         scanf("%d", &post[i]);
61     }
62     int ans = exam(1, N, 1, N);
63     node* root = NULL;
64     create(1, N, 1, N, root);
65     preOrder(root);
66     if(ans == 1)
67         printf("Yes\n");
68     else printf("No\n");
69     for(int i = 0; i < visit.size(); i++){
70         if(i == visit.size() - 1)
71             printf("%d\n", visit[i]);
72         else printf("%d ", visit[i]);
73     }
74     return 0;
75 }
View Code

总结:

1、检验的方法:使用前序、中序递归建立二叉树的方法差不多。传入前序区间和后序区间之后,由前序和后序都可以确定树根。该序列的根合法的情况有:传入区间为空(即空树); 前序确定的根和后序确定的根相同。 不合法的情况:前序与后序确定的树根不同。   然后将该序列划分为左右子树递归判断。有多种划分方法,需要循环。比如序列长为3,则可划分左右子树为(左0, 右3)  (左1, 右2)  (左2,右1)  (左3,右0)

2、需要注意的是,只有当该树的树根合法、左子树与右子树的划分合法,才能构成合法二叉树。划分种类数:左子树个数乘右子树个数。

3、递归建树则对上面的函数稍加改造即可, 核心方法就是找到根的序号并建立新节点存储根。

posted @ 2018-03-04 01:17  ZHUQW  阅读(126)  评论(0编辑  收藏  举报