Construct Binary Tree from Inorder and Postorder Traversal - Problem

Given two integer arrays inorder and postorder representing the inorder traversal and postorder traversal of the same binary tree, your task is to reconstruct and return the original binary tree.

In an inorder traversal, we visit nodes in the pattern: Left → Root → Right
In a postorder traversal, we visit nodes in the pattern: Left → Right → Root

This is a classic tree reconstruction problem that tests your understanding of tree traversal properties and recursive thinking. The key insight is that the last element in postorder is always the root, and we can use the inorder array to determine which elements belong to the left and right subtrees.

Example: If inorder = [9,3,15,20,7] and postorder = [9,15,7,20,3], the tree looks like:

    3
   / \
  9  20
    /  \
   15   7

Input & Output

example_1.py — Standard Tree
$ Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
Output: [3,9,20,null,null,15,7]
💡 Note: The tree structure has 3 as root, 9 as left child, and 20 as right child with its own children 15 and 7. Postorder's last element (3) is the root, and inorder helps us split into left [9] and right [15,20,7] subtrees.
example_2.py — Single Node
$ Input: inorder = [-1], postorder = [-1]
Output: [-1]
💡 Note: A single node tree where the root is -1. Both traversals contain only one element, making it straightforward to construct.
example_3.py — Left Skewed Tree
$ Input: inorder = [1,2,3], postorder = [1,2,3]
Output: [3,2,null,1,null]
💡 Note: This creates a left-skewed tree. Root is 3 (last in postorder), left subtree contains [1,2] from inorder, and there's no right subtree.

Visualization

Tap to expand
Tree Construction from TraversalsInorder: [9,3,15,20,7] | Postorder: [9,15,7,20,3]3Root (last in postorder)920157Left: inorder[9] → node 9Right: inorder[15,20,7] → subtreeHash Map Lookup:value → index in O(1)✨ Build right subtree first, then left (postorder property)
Understanding the Visualization
1
Identify Root
Last element in postorder is always the root of current subtree
2
Find Position
Use hash map to instantly locate root's position in inorder array
3
Split Subtrees
Everything left of root in inorder goes to left subtree, right side goes to right subtree
4
Recursive Build
Build right subtree first (postorder property), then left subtree
Key Takeaway
🎯 Key Insight: Postorder's last element is always the root, and inorder's root position perfectly divides left and right subtrees. Hash map makes root lookups O(1) instead of O(n)!

Time & Space Complexity

Time Complexity
⏱️
O(n)

We visit each node exactly once, and hash map lookups are O(1)

n
2n
Linear Growth
Space Complexity
O(n)

Hash map stores n entries, plus O(n) recursion stack depth

n
2n
Linearithmic Space

Constraints

  • 1 ≤ inorder.length ≤ 3000
  • postorder.length == inorder.length
  • -3000 ≤ inorder[i], postorder[i] ≤ 3000
  • inorder and postorder consist of unique values
  • Each value of postorder also appears in inorder
  • inorder is guaranteed to be the inorder traversal of the tree
  • postorder is guaranteed to be the postorder traversal of the same tree
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
78.5K Views
High Frequency
~25 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