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 preorder and postorder where:
preorder represents the preorder traversal of a binary tree with distinct values
postorder represents the postorder traversal of the same tree

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.

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
🏗️ Binary Tree Archaeological Reconstruction📜 Preorder Documentation(Patriarch → Descendants)Root: 1 (Patriarch)Left heir: 2Left descendants: 4, 5Right heir: 3Right descendants: 6, 7📜 Postorder Documentation(Descendants → Patriarch)Left descendants: 4, 5Left heir: 2Right descendants: 6, 7Right heir: 3Root: 1 (Patriarch)123HashMap Index: O(1) lookupDivide & Conquer: O(n) total
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

n
2n
Quadratic Growth
Space Complexity
O(n)

Recursion stack depth can be O(n) in worst case (skewed tree)

n
2n
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
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
87.5K Views
Medium-High Frequency
~15 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen