Construct Binary Tree from Preorder and Inorder Traversal - Problem

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

Key insights:

  • The first element in preorder 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
  • We can recursively apply this pattern to build the entire tree

Input & Output

Example 1 — Basic Tree
$ Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
💡 Note: Root is 3 (first in preorder). In inorder, 9 is left subtree, [15,20,7] is right subtree. Recursively build: left child 9, right child 20 with children 15,7.
Example 2 — Linear Tree
$ Input: preorder = [1,2,3], inorder = [3,2,1]
Output: [1,2,null,3]
💡 Note: Root 1, left subtree [3,2], no right. Root 2 has left child 3. Forms a left-leaning tree.
Example 3 — Single Node
$ Input: preorder = [1], inorder = [1]
Output: [1]
💡 Note: Single node tree - root is 1 with no children.

Constraints

  • 1 ≤ preorder.length ≤ 3000
  • inorder.length == preorder.length
  • -3000 ≤ preorder[i], inorder[i] ≤ 3000
  • preorder and inorder consist of unique values
  • Each value of inorder also appears in preorder
  • preorder is guaranteed to be the preorder traversal of the tree
  • inorder is guaranteed to be the inorder traversal of the same tree

Visualization

Tap to expand
Construct Binary Tree from Preorder & Inorder INPUT Preorder Traversal 3 9 20 15 7 ROOT Inorder Traversal 9 3 15 20 7 Left Subtree Root Right HashMap (value --> index) 9 --> 0 3 --> 1 15 --> 2 20 --> 3 7 --> 4 ALGORITHM STEPS 1 Build HashMap Map inorder values to indices for O(1) root lookup 2 Get Root from Preorder First element = root (3) preorder[0] is always root 3 Find Root in Inorder map[3] = 1 (index in inorder) Split: left=[9], right=[15,20,7] 4 Recursively Build Build left subtree first Then build right subtree Recursive Construction 3 9 20 FINAL RESULT Constructed Binary Tree 3 9 20 15 7 null null Output (Level Order) [3, 9, 20, null, null, 15, 7] Complexity Analysis Time: O(n) with HashMap Space: O(n) for HashMap Key Insight: HashMap Optimization The first element in preorder is always the root. Finding this root in inorder array divides it into left and right subtrees. Using a HashMap to store inorder indices reduces lookup from O(n) to O(1), making overall time complexity O(n) instead of O(n^2) for naive recursive approach. TutorialsPoint - Construct Binary Tree from Preorder and Inorder Traversal | HashMap Optimization Approach
Asked in
Facebook 45 Amazon 38 Google 32 Microsoft 28
239.2K Views
High Frequency
~25 min Avg. Time
8.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