# PAT Advanced 1119 Pre- and Post-order Traversals

To construct the tree, we can take the 2rd last number in post order as right child, check its pre-order position and see if it can be left child to test uniqueness, in this way, we can solve PAT Advanced 1119 Pre- and Post-order Traversals.

## PAT Advenced 1119 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

## PAT Advenced 1119 Pre- and Post-order Traversals Solution

First we need to see several facts about tree traversals:

- pre-order, post-order, level order, any one of three together with in-order tree traversal, can uniquely determine a binary tree, see this old post for reasoning LeetCode Construct Binary Tree from Inorder and Postorder/Preorder Traversal: DFS. Level order + in-order is similar, as long as we have inorder, we know the root from the level order/pre order/post order, we can know the left subtree and right subtree, and we find the level order/pre order/post order from the given input and recursively construct the binary tree
- post-order plus pre-order can not uniquely determine a binary tree because there are cases where certain nodes could be either left child or the right child, however if we assume this is a full binary tree (all nodes are 2 or zero degree), then we can uniquely determine a binary tree. I just provide the facts, anyone can look it up online for formal mathmatical proof.

Based on the above facts, now we can give the algorithm to solve the problem, the key is the second last element in the post order traversal, we need to check whether this element has to be the right child of the root (that is the last element in the post order traversal), we get the answer by checking the position of the second last element in the pre order traversal, if it is the one just following the root, then it can be either left or right child of the root, otherwise it can only be the right child, we can think about it in this way: if the second last element is the left child of the root, then in pre order, it has to be the exact element after root in the preorder traversal, in both cases, we can safely assume this second last element as the right child of the root and construct the tree.

The following is the C++ source code that could be accepted by PAT OJ to pass this PAT Advenced 1119 Pre- and Post-order Traversals:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#include <cstdio>
#include <vector>
using std::vector;
struct TT {
int v;
TT *l;
TT *r;
TT(int val) : v(val), l(NULL), r(NULL) {}
};
class WW {
private:
bool flag;
vector<int> ans;
public:
WW() : flag(true) {}
TT *bb(int *pre, int *post, int len) {
if (len < 1) {
return NULL;
}
if (len == 1) {
return new TT(*pre);
}
TT *root = new TT(post[len - 1]);
int pc = post[len - 2];
int i = 1;
while (i < len && pre[i] != pc) {
++i;
}
if (i == 1) {
flag = false;
}
root->l = bb(pre + 1, post, i - 1);
root->r = bb(pre + i, post + i - 1, len - i);
return root;
}
public:
void gao(TT *root) {
dfs(root);
if (flag) {
printf("Yes\n");
} else {
printf("No\n");
}
for (int i = 0; i < ans.size(); ++i) {
if (i == 0) {
printf("%d", ans[i]);
} else {
printf(" %d", ans[i]);
}
}
printf("\n");
}
private:
void dfs(TT *root) {
if (NULL == root) {
return;
}
dfs(root->l);
ans.push_back(root->v);
dfs(root->r);
}
};
int main() {
int n;
scanf("%d", &n);
int *pre = new int[n];
int *post = new int[n];
for (int i = 0; i < n; ++i) {
scanf("%d", pre + i);
}
for (int i = 0; i < n; ++i) {
scanf("%d", post + i);
}
WW ww;
TT *r = ww.bb(pre, post, n);
ww.gao(r);
return 0;
}

Note we do not have the build up a explicit binary tree but directly print out the inorder traversal while constructing the tree, think about it yourself.

## Summary

PAT 1119 Pre- and Post-order Traversals, take the 2rd last number in post order as right child, check its pre-order position and see if it can be left child to test uniqueness.