☰ All problems

Construct Binary Tree from Preorder and Inorder Traversal

A legacy analytics system exported its decision tree as two flat lists instead of a proper tree structure:

  • preorder lists the node values in pre-order: a node first, then its left subtree, then its right subtree,
  • inorder lists them in in-order: the left subtree first, then the node, then the right subtree.

Every value in the tree is unique, so the two lists describe exactly one tree. Rebuild it and return its root.

Example 1
Input: preorder = [10,5,2,7,15,20], inorder = [2,5,7,10,15,20]
Output: [10,5,15,2,7,null,20]

Explanation: 10 comes first in pre-order, so it is the root. In inorder, 2,5,7 lie to its left and 15,20 to its right, which splits both lists into the two subtrees. Repeat on each part.

Example 2
Input: preorder = [4,8], inorder = [8,4]
Output: [4,8]

Explanation: 8 comes before the root 4 in inorder, so it is the left child.

Example 3
Input: preorder = [1,2,3], inorder = [1,3,2]
Output: [1,null,2,3]

Explanation: Nothing comes before 1 in inorder, so the root has no left subtree.

Constraints

  • Both arrays have the same length, between 1 and 3000.
  • Values are unique integers from -3000 to 3000.
  • inorder is the in-order traversal of the same tree that preorder describes in pre-order.
💡 Hint 1

The first value in preorder is always the root. Where does that value sit in inorder, and what does that split tell you?

💡 Hint 2

Everything left of the root in inorder is the left subtree, everything right of it is the right subtree. The sizes of those parts tell you how to slice preorder too.

💡 Hint 3

Searching inorder for every root is O(n) each time. Precompute a map from value to in-order index, and consume preorder with a single moving pointer instead of slicing arrays.

/**
 * Definition for a binary tree node (provided):
 * function TreeNode(val, left, right) { this.val = val ?? 0; this.left = left ?? null; this.right = right ?? null; }
 *
 * @param {number[]} preorder
 * @param {number[]} inorder
 * @return {TreeNode}
 */
function buildTree(preorder, inorder) {

}
Ctrl/⌘ + ' run · Ctrl/⌘ + Enter submit
esc