Construct Binary Search Tree from Preorder Traversal
Pattern: Search, insert and delete
Read the preorder left to right. Each call builds the subtree of values below an upper bound: take the next value as the root, build its left subtree with the root as the new bound, then its right subtree with the old bound.
Solve on LeetCodeSolve on GeeksforGeeks
The problem
Rebuild the BST whose preorder traversal is given (values are distinct) and return its root. [8, 5, 1, 7, 10, 12] gives
[8, 5, 10, 1, 7, null, 12]. Trees in the examples are written in level order, with null for a missing child, as on LeetCode.
Think first
- Which value is the root?
- Which of the following values belong to the left subtree?
- How can one pass decide where each value goes?
Hints
Brute force, and why it fails
Insert the values one by one into an empty BST: O(n h), O(n²) on a chain. Sorting to get the inorder and using the general method is O(n log n).
The key insight
Every value has a range it must fit; passing the upper bound down lets each value be placed once, in O(n).
Dry run
| root | 8 |
|---|
preorder [8, 5, 1, 7, 10, 12]. 8 is the root; build its left with bound 8.
The plan
build(bound): if no values left or next > bound: null
root = next; root.left = build(root.val); root.right = build(bound)
Complexity
- Time
O(n)- Space
O(h)
Each value is placed once.
Edge cases
- One value.
- Increasing values: a right chain.
- Decreasing values: a left chain.
I've built my logic, show the code
Java
class Solution {
private int next;
public TreeNode bstFromPreorder(int[] preorder) {
next = 0;
return build(preorder, Integer.MAX_VALUE);
}
private TreeNode build(int[] pre, int bound) { // the subtree of values below bound
if (next == pre.length || pre[next] > bound) return null;
TreeNode root = new TreeNode(pre[next++]);
root.left = build(pre, root.val); // smaller values go left
root.right = build(pre, bound); // the rest, up to the parent's bound, go right
return root;
}
}C++
class Solution {
public:
int next;
TreeNode* bstFromPreorder(vector<int>& preorder) {
next = 0;
return build(preorder, INT_MAX);
}
TreeNode* build(vector<int>& pre, int bound) { // the subtree of values below bound
if (next == static_cast<int>(pre.size()) || pre[next] > bound) return NULL;
TreeNode* root = new TreeNode(pre[next++]);
root->left = build(pre, root->val); // smaller values go left
root->right = build(pre, bound); // the rest, up to the parent's bound, go right
return root;
}
};Python
from lib import TreeNode
class Solution:
def bstFromPreorder(self, preorder: list[int]):
i = 0
def build(bound): # the subtree of values below bound
nonlocal i
if i == len(preorder) or preorder[i] > bound:
return None
root = TreeNode(preorder[i])
i += 1
root.left = build(root.val) # smaller values go left
root.right = build(bound) # the rest, up to the parent's bound, go right
return root
return build(float("inf"))Common mistakes
- Using the parent's value as the bound for the right subtree.
- Re-scanning the array for each subtree.