Construct Binary Tree from Preorder and Inorder Traversal - Problem

Imagine you're given two different blueprints of the same binary tree - one showing the order in which nodes were visited in a preorder traversal (root → left → right), and another showing the order from an inorder traversal (left → root → right).

Your task is to reconstruct the original binary tree using these two traversal sequences. This is like being an architect who needs to rebuild a building from two different perspective drawings!

Key insight: The preorder array tells us which node is the root at each step, while the inorder array tells us which nodes belong to the left vs right subtrees.

Example:
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]

From this, we can deduce that 3 is the root (first in preorder), 9 is the left subtree (everything left of 3 in inorder), and [20,15,7] forms the right subtree.

Input & Output

example_1.py — Standard Tree Construction
$ Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
💡 Note: The tree is constructed with 3 as root (first in preorder), 9 as left child (left of 3 in inorder), and [20,15,7] forming the right subtree. The resulting tree has the structure: 3 at root, 9 as left child, 20 as right child with 15 and 7 as its children.
example_2.py — Single Node Tree
$ Input: preorder = [1], inorder = [1]
Output: [1]
💡 Note: With only one element, the tree consists of just the root node with value 1. Both traversals contain the same single element, confirming this is the only node in the tree.
example_3.py — Linear Tree (Skewed)
$ Input: preorder = [1,2,3], inorder = [3,2,1]
Output: [1,2,null,3,null]
💡 Note: This creates a left-skewed tree. Root is 1, then 2 is the left child of 1, and 3 is the left child of 2. The inorder traversal [3,2,1] confirms this left-heavy structure.

Visualization

Tap to expand
Binary Tree Construction from TraversalsInput Arrays:preorder = [3, 9, 20, 15, 7] ← Root first, then left subtree, then rightinorder = [9, 3, 15, 20, 7] ← Left subtree, then root, then right subtreeConstruction Process3ROOT9LEFT20RIGHT ROOT157Key Insights:1. First element in preorder = ROOT of current subtree2. Root position in inorder = SPLIT POINT for left/right subtrees3. Elements left of split = LEFT SUBTREE, right of split = RIGHT SUBTREE4. Use hash map for O(1) position lookups → Overall O(n) time complexity⚡ Hash map eliminates O(n²) repeated searches!
Understanding the Visualization
1
Identify Root
First element in preorder is always the root of current subtree
2
Find Split Point
Root's position in inorder divides left and right subtrees
3
Calculate Sizes
Count elements to determine subtree boundaries
4
Recursive Build
Recursively construct left and right subtrees
Key Takeaway
🎯 Key Insight: Preorder gives us the construction order (root first), while inorder gives us the structural boundaries (left/right split). A hash map transforms O(n²) searches into O(1) lookups for optimal O(n) performance.

Time & Space Complexity

Time Complexity
⏱️
O(n)

O(n) to build hash map + O(n) for tree construction with O(1) lookups

n
2n
Linear Growth
Space Complexity
O(n)

O(n) for hash map + O(n) for recursion stack in worst case

n
2n
Linearithmic Space

Constraints

  • 1 ≤ preorder.length ≤ 3000
  • inorder.length == preorder.length
  • -3000 ≤ preorder[i], inorder[i] ≤ 3000
  • preorder and inorder consist of unique values
  • inorder is guaranteed to be the inorder traversal of the same tree that preorder is the preorder traversal of
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28 Apple 22
72.4K Views
High Frequency
~18 min Avg. Time
1.8K 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