1050. Construct Binary Search Tree from Preorder Traversal
My accepted Python solution to LeetCode problem 1050, Construct Binary Search Tree from Preorder Traversal, running in 1ms.
- Difficulty: Medium
- Python
- Runtime 1ms
- Memory 17.2MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 1ms, memory 17.2MB, accepted 2026-01-02.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:
if not preorder:
return None
root = TreeNode(preorder[0])
stack = [root]
for i in range(1, len(preorder)):
node = TreeNode(preorder[i])
if preorder[i] < stack[-1].val:
stack[-1].left = node
else:
parent = None
while stack and preorder[i] > stack[-1].val:
parent = stack.pop()
parent.right = node
stack.append(node)
return root