Skip to content
AI360Xpert
Beta
Difficulty: MediumTrees

Construct Binary Tree from Preorder and Inorder Traversal

Problem in Plain English

Given preorder and inorder traversal arrays of a binary tree with unique values, rebuild the tree and return its root. The first preorder value is always the current root, and its position in the inorder array splits the left and right subtrees.

Problem Statement

Given integer arrays preorder and inorder, reconstruct their binary tree and return its root. Preorder visits root, then left subtree, then right subtree; inorder visits left subtree, then root, then right subtree. All values are unique, and both arrays are guaranteed to describe the same valid tree.

Create new nodes with the correct left and right links, without changing either input array. Outputs shown here use level-order notation with null placeholders for missing children; the function returns a node, not an array.

Constraints

  • The traversal arrays each contain between 1 and 3000 values, inclusive, and their lengths are equal.
  • Every value is between -3000 and 3000, inclusive.
  • All values are unique, and both arrays contain the same values.
  • Both arrays are guaranteed valid traversals of the same binary tree.

Examples

Example 1

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

Root 3 splits inorder into left [9] and right [15,20,7]. Preorder supplies 9 for the left leaf and 20 for the right root. Inside that right span, 15 lies left of 20 and 7 lies right, so both become its children.

Example 2

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

The single value -1 is the root. Its inorder split leaves both child spans empty, so it is a leaf with null children.

Example 3

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

Root 1 occurs at the end of inorder, so every remaining value is in its left subtree. The same happens at 2: 3 becomes its left child. Preorder alone would also describe a right chain, but inorder distinguishes the sides.

Example 4

Input
preorder = [2,3,1], inorder = [3,2,1]
Output
[2,3,1]

This is not a binary search tree: 3 is the left child and 1 is the right child. Traversal positions determine the links, not numerical comparisons.

Intuition

Preorder identifies the next subtree root, while inorder tells you which values belong on each side of that root. In Example 1, 3 comes first in preorder; its inorder position separates [9] from [15,20,7]. The left side contains one value, so the next preorder value 9 belongs left and the remaining [20,15,7] belong right. Unique values make every split unambiguous.

Approaches

Naive Recursive Slicing

Solution Details

Reveal Naive Recursive Slicing: intuition, complexity, and code

Hints

Hint 1
The first preorder value is always the root of the subtree being built, so start there.
Hint 2
Find that root inside the inorder array: everything on one side belongs to the left subtree and everything on the other side belongs to the right.
Hint 3
Record inorder positions in a hash map and pass index ranges down the recursion to avoid scanning and slicing on every call.

Edge Cases

  • A singleton, including a negative value, produces a new leaf with null children.
  • A root at the first inorder position has no left subtree; a root at the last position has no right subtree. A chain repeats this split at every depth.
  • A 3000-node left, right, or alternating chain stresses the recursion stack and, for slicing, quadratic copying. Use the stack budget stated with the code.
  • The tree need not be a binary search tree; Example 4 places 3 left of 2.
  • All values are unique and traversals are guaranteed valid. Empty ranges inside the recursion are normal even though the public input is nonempty.
  • Repeated calls must produce independent new nodes and leave both input arrays unchanged; reset the Java map approach cursor on every entry.

Common Mistakes and Interview Tips

  • Starting the left preorder slice at 0 reuses the root; it must start at 1 and contain exactly mid values.
  • Using an inorder absolute index as a local left size when implementing slices mixes coordinate systems.
  • Consuming the right subtree before the left with a shared preorder cursor assigns roots to the wrong spans.
  • Advancing the cursor for an empty inorder range loses a value; test the base case first.
  • Comparing values as if rebuilding a BST is incorrect: only traversal positions determine structure.
  • Claiming O(n) for recursive slicing ignores both repeated scanning and copied arrays; counting only output nodes also misses retained slices.
  • Assuming default runtime stacks support depth 3000 can fail valid inputs; use the documented launch budget and Python wrapper, or implement an explicit frame stack.

Key Takeaway

Preorder names each subtree root while inorder maps its span, so pairing a root stream with a position map rebuilds divided structures in one pass.