https://github.com/emahtab/construct-binary-tree-from-preorder-and-postorder-traversal
Construct Binary Tree from Preorder and Postorder Traversal
https://github.com/emahtab/construct-binary-tree-from-preorder-and-postorder-traversal
binary-tree binary-tree-construction leetcode problem-solving
Last synced: 8 months ago
JSON representation
Construct Binary Tree from Preorder and Postorder Traversal
- Host: GitHub
- URL: https://github.com/emahtab/construct-binary-tree-from-preorder-and-postorder-traversal
- Owner: eMahtab
- Created: 2020-01-12T15:30:38.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-01-19T14:25:53.000Z (almost 6 years ago)
- Last Synced: 2025-02-02T03:26:11.517Z (9 months ago)
- Topics: binary-tree, binary-tree-construction, leetcode, problem-solving
- Homepage:
- Size: 26.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Construct Binary Tree from Preorder and Postorder Traversal
### https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal
Return any binary tree that matches the given preorder and postorder traversals.
Values in the traversals pre and post are distinct positive integers.
```
Example 1:
Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1]
Output: [1,2,3,4,5,6,7]
```
**Note:**
1. 1 <= pre.length == post.length <= 30
2. pre[] and post[] are both permutations of 1, 2, ..., pre.length.
3. It is guaranteed an answer exists. If there exists multiple answers, you can return any of them.
## Thoughts
We can construct a unique binary tree from inorder and preorder sequences, as well as from the inorder and postorder sequences.
But preorder and postorder sequences don’t provide enough information to create a unique binary tree.
More than one binary trees can be constructed due to ambiguity.
```
a a
/ \
b b
/ \
c c
Left Skewed Tree Right Skewed Tree
```
For both left skewed tree and right skewed tree, the preorder and postorder traversal results in same sequence.
```
Preorder [a, b, c]
Postorder [c, b, a]
```
Therefore, its not possible to construct a unique binary tree with the help of preorder and postoder sequences.
However we can construct a unique full binary tree using the Preorder and Postorder traversal.
## Approach :

Preorder : { 1, `2, 4, 5`, `3, 6, 8, 9, 7` }
Postorder : { `4, 5, 2`, `8, 9, 6, 7, 3`, 1 }
We know that root is the first element in preorder sequence and the last element in postorder sequence. Therefore, the root node is 1. Then we locate the next element in preorder sequence, which must be the left child of the root node. In this case, the left child is 2.
Now since 2 is root node of the left subtree, all nodes before 2 in the postorder sequence must be present in the left subtree of the root node i.e. {4, 5, 2} and all the nodes after 2 (except the last) must be present in right subtree i.e. {8, 9, 6, 7, 3}
**Left Subtree**
```
Preorder [2, 4, 5]
Postorder [4, 5, 2]
```
**Right Subtree**
```
Preorder [3, 6, 8, 9, 7]
Postorder [8, 9, 6, 7, 3]
```
## Implementation :
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public static TreeNode constructFromPrePost(int[] pre, int[] post) {
int preStart = 0;
int preEnd = pre.length - 1;
int postStart = 0;
int postEnd = post.length - 1;
Map map = new HashMap<>();
for(int i = 0; i < post.length; i++) {
map.put(post[i], i);
}
return construct(pre, preStart, preEnd, post, postStart, postEnd, map);
}
private static TreeNode construct(int[] pre, int preStart, int preEnd,
int[] post, int postStart, int postEnd, Map map) {
if(preStart > preEnd || postStart > postEnd)
return null;
TreeNode node = new TreeNode(pre[preStart]);
if(preStart+1 <= preEnd) {
int rootIndex = map.get(pre[preStart + 1]);
int leftSubtreeSize = rootIndex - postStart;
node.left = construct(pre, preStart + 1, preStart+1+leftSubtreeSize,
post, postStart, postStart+leftSubtreeSize, map);
node.right = construct(pre, preStart+1+leftSubtreeSize+1, preEnd,
post, rootIndex+1, postEnd - 1, map);
}
return node;
}
}
```
## References :
https://www.techiedelight.com/construct-full-binary-tree-from-preorder-postorder-sequence