# 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:

1. 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
2. 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:

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.

Written on November 29, 2016