Construct Binary Tree from Inorder and Postorder Traversal - Problem

Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.

You may assume that duplicates do not exist in the tree.

A binary tree can be uniquely determined by its inorder and postorder traversals because:

  • The last element in postorder is always the root
  • Elements to the left of root in inorder form the left subtree
  • Elements to the right of root in inorder form the right subtree

Input & Output

Example 1 — Basic Tree
$ Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
Output: [3,9,20,null,null,15,7]
💡 Note: The last element 3 in postorder is the root. In inorder, 3 splits the array: [9] (left subtree) and [15,20,7] (right subtree). Recursively apply the same logic.
Example 2 — Single Node
$ Input: inorder = [-1], postorder = [-1]
Output: [-1]
💡 Note: Only one node, which is both root and the entire tree.
Example 3 — Linear Tree
$ Input: inorder = [1,2,3], postorder = [1,3,2]
Output: [2,1,3]
💡 Note: Root is 2. In inorder: [1] is left subtree, [3] is right subtree. Creates a balanced tree with 2 as root.

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

Visualization

Tap to expand
Binary Tree from Inorder & Postorder INPUT Inorder Traversal: 9 3 15 20 7 [Left, Root, Right] Postorder Traversal: 9 15 7 20 3 [Left, Right, Root] ROOT Hash Map (value --> index): 9 --> 0 3 --> 1 15 --> 2 20 --> 3 7 --> 4 ALGORITHM STEPS 1 Build Hash Map Map inorder values to indices 2 Find Root Last postorder element = root 3 Split Inorder Left/Right subtrees from root 4 Recurse Build right first, then left Inorder Split Example: 9 3 15 20 7 Left Root Right Process: right subtree, then left (matches postorder reverse) FINAL RESULT Constructed Binary Tree: 3 9 20 15 7 Output (Level Order): [3,9,20,null,null,15,7] OK - Tree matches traversals! Time: O(n), Space: O(n) Key Insight: The last element of postorder is always the root. Using a hash map for O(1) index lookup in inorder allows efficient splitting into left/right subtrees. Build right subtree first (reverse postorder order), then left subtree. This recursive approach uniquely reconstructs the tree in O(n) time. TutorialsPoint - Construct Binary Tree from Inorder and Postorder Traversal | Hash Map Optimization
Asked in
Google 42 Amazon 38 Microsoft 35 Facebook 28
287.6K Views
High Frequency
~25 min Avg. Time
3.5K 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