Reconstruct the Binary Search Tree Described by Preorder
Quick Overview
Reconstruct the Binary Search Tree Described by Preorder. Given a valid preorder traversal of a binary search tree with distinct values, construct that tree and return its root.
Reconstruct the Binary Search Tree Described by Preorder
Company: Bloomberg
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
Given a valid preorder traversal of a binary search tree with distinct values, construct that tree and return its root. Every left descendant is strictly smaller than its ancestor and every right descendant strictly greater. Preorder visits root,left subtree,then right subtree.
```cpp
class Solution {
public:
TreeNode* bstFromPreorder(vector<int>& preorder);
};
```
### Examples
**Example 1:**
```text
Input: preorder = [6,2,1,4,9,8]
Output: tree displayed as[6,2,9,1,4,8]
Explanation: Values2,1,4 form the left subtree of6;9 with left child8 forms its right subtree.
```
**Example 2:**
```text
Input: preorder = [3,2,1]
Output: tree displayed as[3,2,null,1]
Explanation: Each next value belongs in the preceding node's left subtree.
```
### Constraints
- `1 <= preorder.length <= 100`.
- `1 <= preorder[i] <= 1000`.
- All values are unique.
- The input is guaranteed to be a valid BST preorder traversal.
### Contract considerations
Preserve the original TreeNode pointer return and supplied node definition; arrays only display example shapes.
```hint Read the boundary condition
The next preorder value belongs to a subtree only if it lies inside all inherited BST bounds.
```
### Boundary reminders
Preserve the original TreeNode pointer return and supplied node definition; arrays only display example shapes.
Overview: Reconstruct the Binary Search Tree Described by Preorder. Given a valid preorder traversal of a binary search tree with distinct values, construct that tree and return its root.
Reconstruct the Binary Search Tree Described by Preorder
Bloomberg
Aug 29, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
Given a valid preorder traversal of a binary search tree with distinct values, construct that tree and return its root. Every left descendant is strictly smaller than its ancestor and every right descendant strictly greater. Preorder visits root,left subtree,then right subtree.
class Solution {
public:
TreeNode* bstFromPreorder(vector<int>& preorder);
};
Examples
Example 1:
Input: preorder = [6,2,1,4,9,8]
Output: tree displayed as[6,2,9,1,4,8]
Explanation: Values2,1,4 form the left subtree of6;9 with left child8 forms its right subtree.
Example 2:
Input: preorder = [3,2,1]
Output: tree displayed as[3,2,null,1]
Explanation: Each next value belongs in the preceding node's left subtree.
Constraints
1 <= preorder.length <= 100
.
1 <= preorder[i] <= 1000
.
All values are unique.
The input is guaranteed to be a valid BST preorder traversal.
Contract considerations
Preserve the original TreeNode pointer return and supplied node definition; arrays only display example shapes.
Boundary reminders
Preserve the original TreeNode pointer return and supplied node definition; arrays only display example shapes.