LeetCode Problem

How to Solve Construct Binary Search Tree from Preorder Traversal

This problem requires reconstructing a binary search tree from its preorder traversal array. The main challenge is correctly placing nodes based on BST properties while efficiently tracking the current state using a stack. Multiple approaches exist, including iterative stack simulation and recursive bounds checking, each with trade-offs in time and space efficiency.

GhostInterview Help

Need help with Construct Binary Search Tree from Preorder Traversal without spending extra time grinding it?

GhostInterview can read Construct Binary Search Tree from Preorder Traversal from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1008Binary-tree traversal and state trackingReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Binary-tree traversal and state tracking
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem requires reconstructing a binary search tree from its preorder traversal array. The main challenge is correctly placing nodes based on BST properties while efficiently tracking the current state using a stack. Multiple approaches exist, including iterative stack simulation and recursive bounds checking, each with trade-offs in time and space efficiency.

Problem Statement

Given an array of unique integers representing the preorder traversal of a binary search tree, construct and return the root node of the corresponding BST. Ensure that for each node, all values in its left subtree are strictly less and all values in its right subtree are strictly greater than the node's value.

The input array always allows the construction of a valid BST. The tree must be reconstructed while maintaining the exact preorder insertion order, reflecting the precise structure implied by the traversal sequence.

Examples

Example 1

Input: preorder = [8,5,1,7,10,12]

Output: [8,5,10,1,7,null,12]

Example details omitted.

Example 2

Input: preorder = [1,3]

Output: [1,null,3]

Example details omitted.

Constraints

  • 1 <= preorder.length <= 100
  • 1 <= preorder[i] <= 1000
  • All the values of preorder are unique.

Solution Approach

Recursive Bounds-Based Insertion

Use recursion with lower and upper bounds to place each node. Maintain an index pointer to traverse the preorder array, creating nodes only if the current value fits within the subtree's bounds. This approach ensures the BST property while directly following preorder ordering.

Iterative Stack Simulation

Maintain a stack to track ancestors of the current node. For each value, pop from the stack until finding the correct parent, then insert as a left or right child. This method avoids recursion, efficiently tracking state and preserving the BST structure.

Optimized Single-Pass Construction

Combine the bounds approach with stack optimization to construct the BST in one pass. Each value is placed using current ancestors from the stack, ensuring O(n) time and minimal auxiliary space. This reduces overhead compared to naive recursive solutions.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity ranges from O(n) for iterative or optimized recursive approaches to potentially O(n^2) if naive insertion is used. Space complexity is O(h) for recursion or stack, where h is the height of the BST, up to O(n) in the worst case.

What Interviewers Usually Probe

  • Watch for correct placement of nodes respecting BST property for left and right subtrees.
  • Expect candidates to optimize recursive calls or avoid unnecessary stack growth.
  • Check handling of edge cases with minimal or maximal preorder values.

Common Pitfalls or Variants

Common pitfalls

  • Inserting nodes without validating BST bounds can violate tree structure.
  • Mismanaging stack updates can lead to incorrect parent-child assignments.
  • Assuming preorder order directly maps to left-right placement without bound checks.

Follow-up variants

  • Construct BST from postorder traversal instead of preorder, reversing insertion logic.
  • Rebuild a BST given both inorder and preorder arrays for precise reconstruction.
  • Determine the height of the BST while constructing it from preorder traversal.

How GhostInterview Helps

  • Guides step-by-step construction of the BST with state tracking and stack management.
  • Highlights iterative and recursive approaches, emphasizing failure modes from preorder insertion errors.
  • Provides optimized strategies for single-pass construction to minimize recursion and auxiliary space.

Topic Pages

FAQ

What is the main challenge in Construct Binary Search Tree from Preorder Traversal?

The challenge is inserting nodes in preorder order while maintaining BST properties, requiring careful state tracking of ancestors.

Can this BST construction be done iteratively?

Yes, using a stack to track parent nodes, each preorder value can be placed in the correct position without recursion.

What is the time complexity of building the BST from preorder?

Optimal approaches achieve O(n) time, visiting each node once, while naive insertion may lead to O(n^2) in worst cases.

Are all values in the preorder array unique?

Yes, uniqueness is guaranteed, simplifying insertion logic since no duplicate handling is needed.

How does the bounds method prevent incorrect BST construction?

By maintaining lower and upper limits for valid node placement, each value is only inserted where it respects BST constraints, ensuring correct tree structure.

GhostInterview Solver

Need direct help with Construct Binary Search Tree from Preorder Traversal instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Construct Binary Search Tree from Preorder Traversal from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.