#1008

Construct Binary Search Tree from Preorder Traversal

Medium Upper-bound recursion

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Rebuild a BST from its preorder traversal.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: rebuild a BST from preorder.

Why this works: give each node an upper limit; a value goes into the current subtree only while it's below that limit.

Example: [8, 5, 1, 7, 10, 12]

8 → left (below 8): 5 → 1, 7right: 10 → 12

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Inserting values one by one — O(n²) for skewed input.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(h).
Topic: Binary Search Trees
🎬 Episodes 61–63: video coming soon