In preorder traversal, the first element is always the root. We find this element's position in the inorder traversal. All elements to the left in inorder are in the left subtree, and all elements to the right are in the right subtree.
- Base case: If
preorderorinorderis empty, return None. root_val = preorder[0].- Create
root = TreeNode(root_val). - Find index of
root_valininorderusing a map for O(1) lookups. - Recursively build left and right subtrees:
- Left subtree uses
inorder[:index] andpreorder[1:index+1]. - Right subtree uses
inorder[index+1:] andpreorder[index+1:].
- Left subtree uses
- Time Complexity: O(N), using a hash map to find indices.
- Space Complexity: O(N) to store the hash map and nodes.
def build_tree(preorder, inorder):
in_map = {val: i for i, val in enumerate(inorder)}
pre_idx = 0
def helper(left, right):
nonlocal pre_idx
if left > right: return None
root_val = preorder[pre_idx]
root = TreeNode(root_val)
pre_idx += 1
# Root splits inorder into left and right subtrees
mid = in_map[root_val]
root.left = helper(left, mid - 1)
root.right = helper(mid + 1, right)
return root
return helper(0, len(inorder) - 1)