Trim a Binary Search Tree
Updated
Problem statement
Given the root of a binary search tree and an inclusive range [low, high], remove every node whose value is outside the range.
Keep the relative structure of retained nodes unchanged. Return the trimmed root; a retained descendant may become the new root.
Function
trimBST(root: TreeNode, low: int, high: int) → TreeNodeExamples
Example 1
root = [1,0,2]low = 1high = 2return = [1,null,2]Removed
Node 0 is below the range and is removed.
Example 2
root = [3,0,4,null,2,null,null,1]low = 1high = 3return = [3,2,null,1]Removed
Moved or new
The branch rooted at 4 is too large. Valid descendants from the left branch remain in BST order.
Example 3
root = [0,null,1]low = 1high = 2return = [1]Removed
Moved or new
The original root is too small, so its retained right child becomes the new root.
Constraints
- The tree contains between
0and100000nodes. -1000000000 <= Node.val, low, high <= 1000000000.low <= high.- All node values are distinct, and the input is a valid binary search tree.
More Bloomberg LP Onsite Interview questions
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Maximum Root-to-Leaf and Leaf-to-Leaf Path SumsAmazon · ONSITE INTERVIEW
- Binary Tree Level Order TraversalGoogle · ONSITE INTERVIEW
- Lowest Common Ancestor Only When Both Targets ExistAdobe · ONSITE INTERVIEW
- Sum Root-to-Leaf NumbersMeta · ONSITE INTERVIEW
- Validate Binary Search TreeGoldman Sachs · ONSITE INTERVIEW
- Balanced Tree Node ReportNetflix · ONSITE INTERVIEW
- Height-Balanced Binary TreeExpedia · ONSITE INTERVIEW
- Binary Tree Target-Sum PathsByteDance · PHONE SCREEN
- Validate Binary Search TreeLyft · PHONE SCREEN
- Lowest Common Ancestor in a Binary TreeZillow · PHONE SCREEN