2Brute2Best
🔥0

Construct Binary Tree from Preorder and Inorder Traversal

Medium·LeetCode #105 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given two arrays that describe the same binary tree.

preorder lists the nodes in preorder: the node, then its left subtree, then its right subtree. inorder lists the nodes in inorder: the left subtree, then the node, then the right subtree.

Rebuild the tree and return its root. All values in the tree are unique.

Input

Two integer arrays, preorder and inorder, of the same length. They come from the same tree, and all values are unique.

Output

The root of the tree that both arrays describe.

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

The first preorder value, 3, is the root. In inorder, the values left of 3 (just 9) form its left subtree. The values right of it (15, 20, 7) form its right subtree, and that subtree's root is the next preorder value, 20.

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

A tree with one node.

Constraints
  • • 1 ≤ preorder.length ≤ 3000
  • • inorder.length == preorder.length
  • • −3000 ≤ preorder[i], inorder[i] ≤ 3000
  • • preorder and inorder consist of unique values.
  • • Each value of inorder also appears in preorder.
  • • preorder is guaranteed to be the preorder traversal of the tree.
  • • inorder is guaranteed to be the inorder traversal of the tree.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“preorder and inorder”
→ Preorder gives each root first. Inorder shows which values sit on each side of that root.
“values are unique”
→ Each value has exactly one inorder index, so a map lookup finds the split point.
“build the tree”
→ A DFS that creates a node and returns it. The order of the calls decides which preorder value each node gets.
03

Approach Complexity Ladder

Brute Force — scan inorder for the root each timeO(N²)O(H)

Take the next preorder value as the root, then search inorder for it with a linear scan

Optimal — hash map of positions and a moving preorder indexO(N)O(N)

Store each value's inorder index in a map once; take roots from preorder in order

Brute Force Intuition

Keep an index into preorder and an inorder range [lo..hi]. Take the next preorder value as the root, find it in the inorder range with a linear scan, and use its position to split the range. Build the left part and the right part the same way.

Recognition rule for this sub-pattern:

Rebuild or reshape a tree; decide the order you visit nodes so pointers are not lost.
Remember this · Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.