Construct Binary Tree From Preorder and Inorder Traversal
Medium · Trees
Jump to codeRebuild a binary tree from valid preorder and inorder arrays containing the same distinct values. Return the root.
Examples
Example 1
preorder = [3,9,20,15,7] inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
Example 2
preorder = [-1] inorder = [-1]
Output: [-1]
Constraints
- 1 <= inorder.length <= 1000
- inorder.length == preorder.length
- -1000 <= preorder[i], inorder[i] <= 1000
Function contract
Trees use level-order arrays, with null for a missing child. Your code receives TreeNode objects with val, left, and right fields.
Need a hint?
Preorder identifies the root; its inorder position splits the subtrees.
Asked in real interviews
Candidates reported versions of Construct Binary Tree From Preorder and Inorder Traversal from these company interviews. Some company questions are locked and open with Premium or XP.
Browse all real coding interview questionsSolution approach
Map each value to its inorder index. Consume preorder with one advancing cursor. For an inorder interval, take the next preorder value as root, recursively build the interval to its left, then the interval to its right. Empty intervals produce null. Avoid repeatedly slicing arrays.
Complexity
O(n) time and O(n) space including the index map and recursion.
Python reference solution
def buildTree(preorder, inorder):
positions = {value: i for i, value in enumerate(inorder)}
cursor = 0
def build(left, right):
nonlocal cursor
if left >= right:
return None
value = preorder[cursor]
cursor += 1
middle = positions[value]
node = TreeNode(value)
node.left = build(left, middle)
node.right = build(middle + 1, right)
return node
return build(0, len(inorder))
JavaScript solution
function buildTree(preorder, inorder) {
const positions = new Map(inorder.map((value, i) => [value, i]));
let cursor = 0;
function build(left, right) {
if (left >= right) return null;
const value = preorder[cursor++], middle = positions.get(value);
const node = new TreeNode(value);
node.left = build(left, middle);
node.right = build(middle + 1, right);
return node;
}
return build(0, inorder.length);
}
Java solution
import java.util.*;
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
Map<Integer, Integer> positions = new HashMap<>();
for (int i = 0; i < inorder.length; i++) positions.put(inorder[i], i);
return build(preorder, positions, new int[]{0}, 0, inorder.length);
}
private TreeNode build(int[] preorder, Map<Integer, Integer> positions,
int[] cursor, int left, int right) {
if (left >= right) return null;
int value = preorder[cursor[0]++], middle = positions.get(value);
TreeNode node = new TreeNode(value);
node.left = build(preorder, positions, cursor, left, middle);
node.right = build(preorder, positions, cursor, middle + 1, right);
return node;
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
TreeNode* buildTree(vector<int> &preorder, vector<int> &inorder) {
unordered_map<int, int> positions;
for (int i = 0; i < (int)inorder.size(); i++) positions[inorder[i]] = i;
int cursor = 0;
function<TreeNode*(int, int)> build = [&](int left, int right) -> TreeNode* {
if (left >= right) return nullptr;
int value = preorder[cursor++], middle = positions[value];
TreeNode* node = new TreeNode(value);
node->left = build(left, middle);
node->right = build(middle + 1, right);
return node;
};
return build(0, inorder.size());
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 9 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
3,
9,
20,
15,
7
],
[
9,
3,
15,
20,
7
]
][ 3, 9, 20, null, null, 15, 7 ]
[
[
-1
],
[
-1
]
][ -1 ]
[
[
1,
2,
3
],
[
2,
1,
3
]
][ 1, 2, 3 ]
[
[
1,
2
],
[
2,
1
]
][ 1, 2 ]
[
[
1,
2,
4,
5,
3
],
[
4,
2,
5,
1,
3
]
][ 1, 2, 3, 4, 5 ]
Drafts are saved per language in this browser when storage is available.