Construct Binary Tree from Preorder and Postorder Traversal - Problem
Reconstruct a Binary Tree from Traversal Blueprints
Imagine you have two different ways of reading a binary tree: preorder (root → left → right) and postorder (left → right → root). Your task is to reconstruct the original binary tree from these two traversal sequences.
Given two integer arrays
•
•
Goal: Reconstruct and return the original binary tree. If multiple valid trees exist, you may return any of them.
Key Insight: Unlike inorder + preorder/postorder combinations, using only preorder + postorder doesn't uniquely determine the tree structure. However, we can still construct a valid binary tree that matches both traversals.
Imagine you have two different ways of reading a binary tree: preorder (root → left → right) and postorder (left → right → root). Your task is to reconstruct the original binary tree from these two traversal sequences.
Given two integer arrays
preorder and postorder where:•
preorder represents the preorder traversal of a binary tree with distinct values•
postorder represents the postorder traversal of the same treeGoal: Reconstruct and return the original binary tree. If multiple valid trees exist, you may return any of them.
Key Insight: Unlike inorder + preorder/postorder combinations, using only preorder + postorder doesn't uniquely determine the tree structure. However, we can still construct a valid binary tree that matches both traversals.
Input & Output
example_1.py — Basic Tree
$
Input:
preorder = [1,2,4,5,3,6,7], postorder = [4,5,2,6,7,3,1]
›
Output:
[1,2,3,4,5,6,7] (tree structure representation)
💡 Note:
The tree has root 1, with left subtree rooted at 2 (containing nodes 4,5) and right subtree rooted at 3 (containing nodes 6,7). The preorder visits root first, then left subtree, then right subtree. The postorder visits left subtree, right subtree, then root.
example_2.py — Simple Case
$
Input:
preorder = [1,2], postorder = [2,1]
›
Output:
[1,2] (tree structure representation)
💡 Note:
Simple tree with root 1 and left child 2. Since there's only one child, it could be either left or right child - our algorithm chooses left by convention.
example_3.py — Single Node
$
Input:
preorder = [1], postorder = [1]
›
Output:
[1]
💡 Note:
Edge case with only one node - both traversals contain just the root node 1.
Visualization
Tap to expand
Understanding the Visualization
1
Map the Territory
Create an index map of all elements in postorder for instant boundary detection
2
Identify the Patriarch
The first element in preorder is always the root of current subtree
3
Find the Heir
The second element in preorder (if exists) is the root of left subtree
4
Divide the Kingdom
Use postorder indices to determine where left subtree ends and right begins
5
Recursive Reconstruction
Apply the same process recursively to build left and right subtrees
Key Takeaway
🎯 Key Insight: By preprocessing postorder indices into a hash map, we transform expensive O(n) boundary searches into O(1) lookups, making the entire reconstruction process run in optimal O(n) time while maintaining the elegant divide-and-conquer structure.
Time & Space Complexity
Time Complexity
O(n²)
For each of n nodes, we potentially scan the postorder array to find subtree boundaries
⚠ Quadratic Growth
Space Complexity
O(n)
Recursion stack depth can be O(n) in worst case (skewed tree)
⚡ Linearithmic Space
Constraints
- 1 ≤ preorder.length ≤ 30
- 1 ≤ postorder.length ≤ 30
- preorder.length == postorder.length
- 1 ≤ preorder[i], postorder[i] ≤ 106
- preorder and postorder are both permutations of the same set of distinct values
- preorder and postorder represent valid traversals of the same binary tree
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code