LeetCode #105 Medium

Construct BT from Preorder and Inorder

Rebuild the unique binary tree from its preorder and inorder traversals.

treedivide-and-conquerhash-table
Open on LeetCode ↗
02

Intuition

Preorder's first element is always the root. Find it in inorder: everything left of it is the left subtree, everything right the right subtree. Recurse with the matching slices. A value→index map makes each root lookup O(1).

How to spot this pattern

Preorder hands you roots in order; inorder tells you where each root splits its subtree. The hash map from value to inorder index turns the "find the root" step from a linear scan into O(1), which is the difference between O(n²) and O(n).

03

Approach

1

Root from preorder, split from inorder

pre[0] = root; its inorder position k splits inorder into left (size k) and right parts.

2

Slice preorder to match

The next k preorder elements belong to the left subtree, the rest to the right — same sizes as the inorder split.

3

Index map + pointers

Precompute value → inorder index; pass array bounds instead of copying slices → O(n) total.

04

Solution & live demo

1class Solution:
2 def buildTree(self, preorder, inorder):
3 idx = {v: i for i, v in enumerate(inorder)}
4 self.pre = 0
5 def build(lo, hi): # inorder bounds
6 if lo > hi: return None
7 val = preorder[self.pre]; self.pre += 1
8 node = TreeNode(val)
9 node.left = build(lo, idx[val] - 1)
10 node.right = build(idx[val] + 1, hi)
11 return node
12 return build(0, len(inorder) - 1)
05

Common pitfalls

Searching the inorder array each time

✗ Wrong
mid = inorder.index(val)
✓ Right
idx = {v: i for i, v in enumerate(inorder)}
... idx[val]

A linear search at every node makes the build O(n²), which times out on a skewed tree of 3,000 nodes. Precomputing positions once makes each lookup constant.

Passing the preorder index by value

✗ Wrong
def build(lo, hi, pre):
    val = preorder[pre]
    node.left = build(lo, mid - 1, pre + 1)
✓ Right
self.pre = 0
...
val = preorder[self.pre]; self.pre += 1

The left subtree consumes an unknown number of preorder entries, so the right subtree's starting index isn't pre + 1 — it depends on how many nodes the left call used. A shared cursor advances correctly without that arithmetic.

Building the right subtree first

✗ Wrong
node.right = build(idx[val] + 1, hi)
node.left = build(lo, idx[val] - 1)
✓ Right
node.left = build(lo, idx[val] - 1)
node.right = build(idx[val] + 1, hi)

Preorder lays out root, then the entire left subtree, then the right. A shared cursor must consume them in that same order, so left has to be built first — the opposite of the postorder variant.

06

Edge cases

Skewed tree

One side of every split is empty; recursion depth n.

Duplicate values

Problem guarantees uniqueness — the split would be ambiguous otherwise.

07

Complexity

Time
O(n)
Space
O(n)
Hash map + single preorder pointer.