Construct Binary Tree from Preorder and Inorder Traversal

Difficulty: Medium

You're given two traversals of the same binary tree: preorder (node, left, right) and inorder (left, node, right). All values are unique.

Rebuild the tree and return its root.

Examples

Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]

Preorder always starts with the root, so 3 is the root. In inorder, everything left of 3 (just [9]) is the left subtree, and everything right of it ([15,20,7]) is the right subtree.

Input: preorder = [-1], inorder = [-1]
Output: [-1]

A single-node tree.

Constraints

  • 1 <= preorder.length <= 3000

  • inorder.length == preorder.length

  • All values in preorder and inorder are unique, and every value in inorder also appears in preorder.

Approach

Preorder's first element is the current subtree's root. Find it in inorder: everything left of it is the left subtree, and everything right of it is the right subtree. Recurse on both pieces to rebuild the tree.

The direct version searches inorder and copies sub-arrays on every call, which adds up. The optimized version precomputes each value's position in inorder and tracks subtree boundaries with indices instead of copies.

Solutions

Brute Force — Search and Slice

Take the first preorder element as the root, find it in inorder with indexOf, and slice both arrays into left and right parts for the recursive calls. Simple, but the repeated searching and copying adds real overhead.

class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function buildTree(preorder, inorder) {
  if (preorder.length === 0) return null;

  const rootVal = preorder[0];
  const root = new TreeNode(rootVal);

  const mid = inorder.indexOf(rootVal);
  root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
  root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1));

  return root;
}

Time: O(n^2) — indexOf and slice each cost O(n), across O(n) calls · Space: O(n^2) — repeated array copies from slicing

Optimal — Index Map with Boundary Pointers

First, map each value to its index in inorder, so finding the split is instant instead of a linear search.

Instead of slicing, track the current subtree with a range of indices into inorder, plus one shared pointer into preorder for the next root. Preorder never needs slicing, because its values are used strictly in order as you recurse.

class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function buildTree(preorder, inorder) {
  const indexOfInorder = new Map();
  inorder.forEach((val, idx) => indexOfInorder.set(val, idx));

  let preorderIndex = 0;

  function build(inorderLeft, inorderRight) {
    if (inorderLeft > inorderRight) return null;

    const rootVal = preorder[preorderIndex];
    preorderIndex++;

    const root = new TreeNode(rootVal);
    const mid = indexOfInorder.get(rootVal);

    root.left = build(inorderLeft, mid - 1);
    root.right = build(mid + 1, inorderRight);
    return root;
  }

  return build(0, inorder.length - 1);
}

Time: O(n) · Space: O(n) — the index map, plus O(h) recursion stack