Construct Binary Tree from Preorder and Inorder Traversal
Problem Statement
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.
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 tree
Input Format:
- Two integer arrays preorder and inorder
Output Format:
- Return the root of the constructed binary tree
Examples:
Example 1:
Input:
preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output:
[3,9,20,null,null,15,7]
Explanation:
This creates a binary tree with root value 3, left child 9, and right child 20. Node 20 has left child 15 and right child 7.
Example 2:
Input:
preorder = [-1], inorder = [-1]
Output:
[-1]
Explanation:
This creates a binary tree with only a root node with value -1.
Example 3:
Input:
preorder = [1,2], inorder = [2,1]
Output:
[1,2]
Explanation:
This creates a binary tree with root value 1 and left child 2.
Solutions
Recursive Approach with HashMap
Use the preorder traversal to determine the root nodes and the inorder traversal to determine the left and right subtrees. Use a HashMap for quick lookup of inorder indices.
Iterative Approach with Stack
Use a stack to simulate the recursive approach, processing nodes level by level while keeping track of the current position in both traversals.
Algorithm Walkthrough
Example input: nums = []
Step-by-step execution:
- Create a map for inorder traversal: {9:0, 3:1, 15:2, 20:3, 7:4}
- Start building the tree with root = 3 (preorder[0])
- Find the index of 3 in inorder: 1
- Left subtree: preorder[1:2], inorder[0:0] = [9], [9]
- Build left subtree of 3 recursively
- Root = 9 (preorder[1])
- No more elements in left subtree, return 9 as left child of 3
- Right subtree: preorder[2:4], inorder[2:4] = [20, 15, 7], [15, 20, 7]
- Build right subtree of 3 recursively
- Root = 20 (preorder[2])
- Find the index of 20 in inorder: 3
- Left subtree: preorder[3:3], inorder[2:2] = [15], [15]
- Build left subtree of 20 recursively
- Root = 15 (preorder[3])
- No more elements in subtree, return 15 as left child of 20
- Right subtree: preorder[4:4], inorder[4:4] = [7], [7]
- Build right subtree of 20 recursively
- Root = 7 (preorder[4])
- No more elements in subtree, return 7 as right child of 20
- Return 20 as right child of 3
- Final tree: [3,9,20,null,null,15,7]
Hints
Hint 1
Hint 2
Hint 3
Hint 4
Video Tutorial
Video tutorials can be a great way to understand algorithms visually
Visualization
Visualize how the binary tree is constructed using preorder and inorder traversals.
Key visualization elements:
- current preorder node
- current inorder node
- left subtree
- right subtree
Implementation Notes
This problem tests understanding of tree traversals and tree construction algorithms. It is a classic interview question that combines array manipulation, divide-and-conquer strategy, and binary tree concepts.