FastPrepTrim a Binary Search Tree
Problems/Trim a Binary Search Tree

Trim a Binary Search Tree

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names this well-known problem directly in the Bloomberg new-grad virtual onsite. We used the standard version, where both ends of the range are included.

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) → TreeNode

Examples

Example 1

root = [1,0,2]low = 1high = 2return = [1,null,2]
root
102
Removed
→
return
12

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]
root
30214
Removed
→
return
321
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]
root
01
Removed
→
return
1
Moved or new

The original root is too small, so its retained right child becomes the new root.

Constraints

  • The tree contains between 0 and 100000 nodes.
  • -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
See Bloomberg LP hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode trimBST(TreeNode root, int low, int high) {
        // Write your code here.
    }

}
Checking account…