Last Updated: April 1, 2026
We have two arrays that describe the same binary tree from different perspectives. Preorder traversal visits root, then left subtree, then right subtree. Inorder traversal visits left subtree, then root, then right subtree.
The question is: can we combine these two perspectives to uniquely reconstruct the original tree? The answer is yes, and the reasoning is elegant.
The preorder array always tells us the root first. But preorder alone cannot tell us where the left subtree ends and the right subtree begins (unlike a BST where values give us that split). That is where inorder comes in. Once we know the root, we can find it in the inorder array, and everything to its left in inorder belongs to the left subtree, everything to its right belongs to the right subtree. This gives us the split point we need.
So the two traversals complement each other perfectly: preorder identifies the root, and inorder tells us the subtree boundaries.
1 <= preorder.length <= 3000 → With n up to 3000, an O(n^2) approach runs at most ~9 million operations, which is fine. But we can do O(n) with a hash map optimization.-3000 <= preorder[i], inorder[i] <= 3000 → Values can be negative. No impact on the algorithm, but good to note.The most natural approach comes from understanding what each traversal tells us.
Preorder gives us the root immediately, it is always the first element. But it does not tell us where the left subtree ends and the right subtree begins. In a BST, the values themselves tell us this (everything smaller goes left), but in a general binary tree, we cannot rely on that.
This is where inorder comes in. Once we know the root value from preorder, we can find it in the inorder array. Everything to the left of the root's position in inorder belongs to the left subtree, and everything to the right belongs to the right subtree. This is exactly by definition of inorder traversal: left, root, right.
Here is the really useful part: once we know the left subtree has, say, k nodes (from the inorder split), we also know the next k elements in the preorder array after the root form the left subtree's preorder. The remaining elements form the right subtree's preorder.
So we recursively apply the same logic: pick the root from preorder, find it in inorder to get the split, and recurse on both halves.
This approach works correctly, but for every node we create, we scan the inorder array to find the root's position. What if we pre-computed every value's position so lookups were O(1)?
The algorithm from Approach 1 is fundamentally correct. The only thing slowing it down is the linear search to find the root in the inorder array. If we could look up any value's index in O(1), each recursive call would do O(1) work, and the total time would drop to O(n).
A hash map is the perfect tool for this. Before we start building the tree, we iterate through the inorder array once and map each value to its index. Then during recursion, finding the root's position in inorder becomes a single hash map lookup instead of a scan.
Everything else stays the same: preorder tells us the root, inorder (via the hash map) tells us the split, and we recurse on both halves.
The key insight is that preorder and inorder traversals encode complementary information. Preorder always reveals the root first, while inorder reveals the boundary between left and right subtrees. Together, they uniquely determine the tree structure.
The hash map does not change the algorithm at all, it just replaces the O(n) linear search with an O(1) lookup. Since we build the map once in O(n) and each of the n recursive calls does O(1) work, the total time is O(n).