Construct Binary Search Tree from Preorder Traversal Problem
Description
LeetCode Problem 1008.
Given an array of integers preorder, which represents the preorder traversal of a BST (i.e., binary search tree), construct the tree and return its root.
It is guaranteed that there is always possible to find a binary search tree with the given requirements for the given test cases.
A binary search tree is a binary tree where for every node, any descendant of Node.left has a value strictly less than Node.val, and any descendant of Node.right has a value strictly greater than Node.val.
A preorder traversal of a binary tree displays the value of the node first, then traverses Node.left, then traverses Node.right.
Example 1:

Input: preorder = [8,5,1,7,10,12]
Output: [8,5,10,1,7,null,12]
Example 2:
Input: preorder = [1,3]
Output: [1,null,3]
Constraints:
1 <= preorder.length <= 1001 <= preorder[i] <= 1000- All the values of
preorderare unique.
Solution
Approach: Recursion with an Upper Bound
In a preorder traversal, the first value is the root, then comes the whole left subtree, then the whole right subtree. In a BST, the left subtree is exactly the values smaller than the root. So after the root, the left subtree is the run of following values that are smaller than the root, and the right subtree starts at the first value that is larger.
Instead of searching for where the left subtree ends, we read the array once, left to right, with a shared index i, and give each recursive call an upper bound:
build(bound)builds a subtree out of the next values, as long as they are less thanbound. If the next value is larger thanbound(or there are none left), the subtree is empty.- Otherwise the next value becomes the root. Its left subtree is
build(root.val): it takes the following values while they are smaller than the root. Its right subtree isbuild(bound): the values bigger than the root but still under the parent's bound.
We start with build(infinity). Each value is turned into a node exactly once, and every call either makes a node or returns right away, so the whole array is consumed in one pass.
For [8,5,1,7,10,12]: 8 is the root; 5, 1, 7 are under 8 and form its left subtree (5 with left child 1 and right child 7, since 7 is above 5 but under 8); then 10 stops that subtree and becomes the right child of 8, with 12 as its right child.
Complexity: O(n) time, and O(h) extra space for the recursion, where h is the height of the tree (up to n for a sorted input).
class Solution:
def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:
i = 0 # next value of preorder to place
def build(bound: int) -> Optional[TreeNode]:
"""Builds the subtree made of the next values that are less than bound."""
nonlocal i
if i == len(preorder) or preorder[i] > bound:
return None
root = TreeNode(preorder[i])
i += 1
root.left = build(root.val) # the left subtree holds values below root.val
root.right = build(bound) # the rest, up to the bound, goes right
return root
return build(float("inf"))