Construct BST from Preorder
A medium BST problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- BST
- Sheets
- 1
- Core for
- 2 roles
- Platform
- LeetCode
The problem
Given an integer array representing the preorder traversal of a binary search tree, construct and return the BST. Each element in preorder appears in the order they would be visited in a preorder traversal.
Example 1
- Input
- preorder=[8,5,1,7,10,12]
- Output
- [8,5,10,1,7,null,12]
- Why
- 8 is root, 5 and 1 are left subtree, 10 and 12 are right subtree. 7 is right child of 5.
Example 2
- Input
- preorder=[1,2,3]
- Output
- [1,null,2,null,3]
- Why
- Elements are in ascending order, forming a right-skewed tree.
Example 3
- Input
- preorder=[3,2,1]
- Output
- [3,2,null,1]
- Why
- Elements are in descending order, forming a left-skewed tree.
Constraints
- 1 <= preorder.length <= 100
- 1 <= preorder[i] <= 104
- All values in preorder are unique.
How to think about it
Updated 2026-09-09In preorder, the first element is always the current root. All subsequent elements smaller than the root belong to its left subtree, and all elements larger belong to its right subtree. Instead of scanning for that partition point at each level, pass an upper bound constraint: a node consumes the next preorder token if and only if it is smaller than its inherited upper bound.
Approaches, worst first
Sort preorder to obtain inorder
time O(n log n) · space O(n)
Sort the preorder array to get the tree's inorder sequence, then construct the binary tree using the standard preorder-and-inorder reconstruction algorithm. Works correctly but incurs sorting overhead and hash map lookups.
Recursive partitioning
time O(n^2) · space O(n)
Take the first element as root, scan linearly for the first element greater than root to split into left and right subtrees, and recurse. On skewed trees, repeated linear scans degrade runtime to quadratic.
Upper bound validation passWrite this one
time O(n) · space O(h)
Maintain a global preorder index and recursively build nodes while passing an upper limit. If the next preorder value exceeds the bound, return null. The left branch inherits the current node's value as its bound; the right branch keeps the parent's bound.
Where people lose marks · 2
- Unbounded right branches: passing an incorrect upper limit to the right child lets elements belonging to higher ancestors be greedily absorbed into the wrong subtree.
- Index increment desynchronization: incrementing the array index before confirming the value falls within the valid bound skips valid elements for ancestor branches.
The theory behind it
BST — the ground this problem stands on. All BST problems
What BST is
A binary search tree is a binary tree that enforces a strict ordering rule at every node. Every value stored in a node's left branch must be smaller than the node itself, and every value in its right branch must be larger. Because this rule holds true everywhere down the tree, searching for a value does not require checking every branch. At each step, a single comparison lets you discard an entire half of the remaining nodes.
When to reach for it
Reach for a binary search tree when problems mention sorted tree structures, finding the kth smallest element, checking tree validity, or searching within a range of values. Signals include queries for the next greater element in a dynamic set, finding the lowest common ancestor in a sorted tree, or converting sorted arrays into height-balanced trees. When data must remain sorted while supporting continuous insertions and lookups, BST properties are the target.
How the pattern works
Use the ordering property to prune entire subtrees. When looking for a key, move left if the target is smaller than the current node, or move right if it is larger, stopping when values match or when reaching a null link. For validating a tree, do not merely compare a node to its immediate children; pass valid lower and upper value bounds down through recursive calls. Walking a valid binary search tree in-order visits all values in strictly increasing numerical order.
What each operation costs
| Operation | Time |
|---|---|
| search, insert, or delete on balanced tree | O(log n) |
| search, insert, or delete on degenerate line tree | O(n) |
| in-order traversal visiting all nodes in sorted order | O(n) |
What usually goes wrong with BST
- Validating a tree by checking only immediate children against the parent, missing violations where a node deep in the left subtree is larger than an ancestor higher up.
- Allowing duplicate values on the wrong side when the problem specification requires strictly smaller values on the left and strictly greater on the right.
- Deleting a node with two children by removing it directly instead of replacing its value with its in-order predecessor or successor and deleting that leaf instead.
Which roles need this problem
BST is a core topic for these 2 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 6 more roles, including Full-Stack Developer, Android Developer, iOS Developer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore BST problems
Problem set and role mapping as of .