Description
Construct Binary Tree from Preorder and Inorder Traversal
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.
Example 1:

Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
Example 2:
Input: preorder = [-1], inorder = [-1]
Output: [-1]
Constraints:
1 <= preorder.length <= 3000inorder.length == preorder.length-3000 <= preorder[i], inorder[i] <= 3000preorderandinorderconsist of unique values.- Each value of
inorderalso appears inpreorder. preorderis guaranteed to be the preorder traversal of the tree.inorderis guaranteed to be theinordertraversal of the tree.
Approach 1: Recursive Divide & Conquer with HashMap ( Time, Space)
Intuition
- The first element of
preorderis always the root of the tree/subtree. - Locating that root element in
inordersplits the tree:- Everything to the left of the root index in
inorderbelongs to the left subtree. - Everything to the right belongs to the right subtree.
- Everything to the left of the root index in
- We store all
inordervalue-to-index mappings in aHashMapupfront for lookups. - Recursively build the left subtree first (since
preorderprocesses left children next), then the right subtree.
import java.util.HashMap;
import java.util.Map;
class Solution {
private int preIndex = 0;
private Map<Integer, Integer> inorderMap;
public TreeNode buildTree(int[] preorder, int[] inorder) {
inorderMap = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
inorderMap.put(inorder[i], i);
}
return build(preorder, 0, inorder.length - 1);
}
private TreeNode build(int[] preorder, int left, int right) {
if (left > right) return null;
int rootVal = preorder[preIndex++];
TreeNode root = new TreeNode(rootVal);
int inorderIndex = inorderMap.get(rootVal);
// Build left subtree first, then right subtree
root.left = build(preorder, left, inorderIndex - 1);
root.right = build(preorder, inorderIndex + 1, right);
return root;
}
}
Complexity
- Time Complexity: — Pre-building the
inorderMaptakes , and each node is processed exactly once in time. - Space Complexity: — space for the
HashMapplus recursion stack space ( in worst case for skewed trees).
Approach 2: Iterative Stack ( Time, Space)
Intuition
Simulate the preorder traversal sequence using a stack and an inorderIndex pointer:
- Push
preorder[0]as the root onto the stack. - Iterate through
preorderstarting from index1:- If stack top
inorder[inorderIndex]: The next element is a left child of the current node. Attach it tonode.leftand push it onto the stack. - If stack top
inorder[inorderIndex]: We’ve reached the bottom of a left branch. Pop nodes from the stack while they matchinorder[inorderIndex]. The next element is the right child of the last popped node. Attach it and push it onto the stack.
- If stack top
import java.util.ArrayDeque;
import java.util.Deque;
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder.length == 0) return null;
TreeNode root = new TreeNode(preorder[0]);
Deque<TreeNode> stack = new ArrayDeque<>();
stack.push(root);
int inorderIndex = 0;
for (int i = 1; i < preorder.length; i++) {
int val = preorder[i];
TreeNode node = stack.peek();
if (node.val != inorder[inorderIndex]) {
node.left = new TreeNode(val);
stack.push(node.left);
} else {
while (!stack.isEmpty() && stack.peek().val == inorder[inorderIndex]) {
node = stack.pop();
inorderIndex++;
}
node.right = new TreeNode(val);
stack.push(node.right);
}
}
return root;
}
}
Complexity
- Time Complexity: — Each element in
preorderis pushed and popped from the stack at most once. - Space Complexity: — The stack stores at most tree nodes.
Easy Memory Rule
“
preorder[0]is Root Find Root ininorderusingHashMapLeft of Root = Left Subtree, Right of Root = Right Subtree!”