Construct BST from Preorder Traversal
Construct Binary Search Tree from Preorder Traversal is LeetCode 1008 (Medium). Given the preorder traversal of a binary search tree (node, then left subtree, then right subtree), rebuild the tree and return its root.
- All values are distinct, and a valid BST always exists.
- In a BST every value in a left subtree is smaller than the node, and every value in a right subtree is larger.
The array holds at most 100 values, so slower methods pass too, but an O(n) build is the expected answer.
- 1 <= preorder.length <= 100
- 1 <= preorder[i] <= 1000
- All the values of preorder are unique.
Intuition
Preorder lists a node first, then all of its left subtree, then all of its right subtree. In a BST the left subtree holds exactly the values below the node, so as you read the array from left to right, the left subtree is the run of values that stay below the node's value.
So give each recursive call an upper limit, and let it take values only while they stay under it:
- Left child's limit: the node's own value.
- Right child's limit: the limit the node itself was given.
The first value over the limit is not lost: it belongs to an ancestor's right side, and the call that owns it picks it up. Each value is read once, so the build is O(n).
Rebuilding a tree from one traversal works when the values themselves say where each one goes, as the BST order does here. Passing a bound down the recursion is the same tool as in Validate Binary Search Tree.
Approach
Before reading on: for [8, 5, 1, 7, 10, 12], which values make up the left subtree of 8, and how could you tell while reading the array once, without searching ahead for the first value above 8?
Two ways to solve it
Each call takes values while they stay under its bound, then hands the rest back to its caller.
- Reads: each value once.
- Code: one short helper and a shared index.
- Depth: the recursion is as deep as the tree.
The shortest O(n) answer to explain.
Keep the nodes that may still get a right child on a stack, and pop to find each new value's parent.
- Reads: each value once; each node popped at most once.
- Code: one loop, no recursion.
- Depth: no call stack to overflow.
The usual iterative follow-up.
Both run in O(n); the bound recursion is shorter and follows the BST definition directly, so the steps, code and live demo below use it. The stack version does the same work without recursion; its code comes after the demo.
Read the array with one shared index
Keep one index i into preorder, shared by every call. A call either builds a node from preorder[i] and moves i on, or builds nothing. Because the index is shared, each call starts exactly where the previous one stopped.
Return nothing when the next value does not fit
build(bound) returns None without reading when:
ihas reached the end of the array, orpreorder[i] > bound: that value is too big for this subtree, and an ancestor will place it on its right.
Build the node, then both subtrees
Make a node from preorder[i] and move i on. Build node.left with the node's value as the bound, since only smaller values belong there. Then build node.right with the bound this call received: those values are above the node, but still have to fit wherever this node sits.
Start with no limit
Call build(infinity): the root takes the first value, and nothing in the tree is too big for it. Return the node it builds.
Code in Python, C++ and Java
Stack of open ancestors
The stack holds nodes that may still get a right child, with values decreasing from bottom to top. For each new value, pop every node smaller than it: the last one popped is its parent, and it goes on the right. If nothing is popped, it is the left child of the top node.
Common pitfalls
Giving the right child the node's value as its bound
node.left = build(node.val) node.right = build(node.val)
node.left = build(node.val) node.right = build(bound)
The right side then only accepts values below the node, which belong on its left. On [8, 5, 1, 7, 10, 12], 7 is hung on the right of 8, and 10 and 12 are never placed.
Passing the index by value
def build(i, bound):
...
node.left = build(i + 1, node.val)
node.right = build(i + 1, bound)nonlocal i # one index shared by every call
The right subtree has to start where the left subtree stopped reading, and a copied index cannot know that. Both children start at i + 1, so values are read again: on [8, 5, 1, 7, 10, 12], 5 turns up as both children of 8.
Edge cases
The tree is a chain of height n, so the recursion is n calls deep. With at most 100 values that is fine.
Complexity
Construct BST from Preorder Traversal FAQ
Why is the preorder alone enough to rebuild the tree?
For a general binary tree, preorder does not say where the left subtree ends, so you also need the inorder traversal. In a BST the values say it: the left subtree is exactly the values below the node, so preorder alone fixes the tree, and only one tree fits.
Are there simpler ways to build the tree?
- Insert one by one: run normal BST insertion for each value. O(n²) when the input is sorted, since each insertion walks the whole chain.
- Split at the first larger value: the root is
preorder[0], the left subtree is everything before the first value above it, the right subtree is the rest. O(n²) with a linear scan, O(n log n) with binary search.
Both pass with 100 values, but the bound method is O(n).