Bird
Raised Fist0
Interview Prepbinary-search-treesmediumAmazonGoogle

Trim a BST

Choose your preparation mode3 modes available
📋
Problem

Imagine you have a large database of sorted records represented as a BST, but you only want to keep records within a certain range, discarding all others efficiently.

Given the root of a binary search tree and two integers low and high, trim the tree so that all its elements lie in [low, high]. Trimming the tree means removing nodes whose values are less than low or greater than high. Return the root of the trimmed binary search tree. The resulting tree should still be a valid BST.

The number of nodes in the tree is in the range [1, 10^5].0 <= Node.val <= 10^90 <= low <= high <= 10^9
Edge cases: All nodes are within the range → output is the original treeAll nodes are outside the range → output is nullOnly root node is within the range → output is a single node tree
</>
IDE
def trimBST(root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:public TreeNode trimBST(TreeNode root, int low, int high)TreeNode* trimBST(TreeNode* root, int low, int high)function trimBST(root, low, high)
def trimBST(root, low, high):
    # Write your solution here
    pass
class Solution {
    public TreeNode trimBST(TreeNode root, int low, int high) {
        // Write your solution here
        return null;
    }
}
#include <vector>
using namespace std;

TreeNode* trimBST(TreeNode* root, int low, int high) {
    // Write your solution here
    return nullptr;
}
function trimBST(root, low, high) {
    // Write your solution here
}
0/9
Common Bugs to Avoid
Wrong: Returns original tree without pruning nodes outside rangeMissing recursive pruning or incorrect subtree replacement when node.val out of rangeAdd recursive calls to trim left and right subtrees and return trimmed subtree based on node.val compared to low and high
Wrong: Prunes nodes equal to low or high valuesUsing strict inequalities (< or >) instead of inclusive (<= or >=) in range checksChange conditions to include equality: if node.val < low return right subtree, if node.val > high return left subtree
Wrong: Fails on empty tree input or single node treesNo base case handling for null root or single nodeAdd base case: if root is None return None; check node.val for single node
Wrong: Only prunes immediate children, missing deeper nodes outside rangeGreedy pruning without recursion on subtreesUse recursive calls to trim entire left and right subtrees fully
Wrong: Algorithm times out on large inputsUsing brute force or rebuilding tree multiple times instead of pruning in O(n)Implement recursive pruning using BST properties to achieve O(n) time complexity
Test Cases
t1_01basic
Input{"root":[3,0,4,null,2,null,null,1],"low":1,"high":3,"n":7}
Expected[3,2,null,1]

Nodes with values 0 and 4 are removed because they are outside the range [1,3]. The resulting tree keeps nodes 3, 2, and 1.

t1_02basic
Input{"root":[5,3,8,2,4,6,10],"low":4,"high":9,"n":7}
Expected[5,4,8,null,null,6]

Nodes 2,3,10 are removed because they are outside [4,9]. The trimmed tree keeps nodes 5,4,8,6.

t2_01edge
Input{"root":[],"low":0,"high":10,"n":0}
Expected[]

Empty tree input returns empty tree output.

t2_02edge
Input{"root":[5],"low":4,"high":6,"n":1}
Expected[5]

Single node tree with value inside range returns the same single node tree.

t2_03edge
Input{"root":[5],"low":6,"high":10,"n":1}
Expected[]

Single node tree with value outside range returns null.

t3_01corner
Input{"root":[10,5,15,3,7,null,18],"low":7,"high":15,"n":7}
Expected[10,7,15]

Nodes 3,5,18 are pruned. Greedy approach that only prunes immediate children fails here.

t3_02corner
Input{"root":[5,3,7,2,4,6,8],"low":4,"high":7,"n":7}
Expected[5,4,7,null,null,6]

Confusing 0/1 vs unbounded pruning: nodes 2 and 8 are pruned correctly, tree remains valid BST.

t3_03corner
Input{"root":[8,3,10,1,6,null,14,null,null,4,7,13],"low":5,"high":13,"n":12}
Expected[8,6,10,null,7,null,13]

Off-by-one errors in boundary checks cause incorrect pruning of nodes with values equal to low or high.

t4_01performance
Input{"root":[50000,25000,75000,12500,37500,62500,87500,6250,18750,31250,43750,56250,68750,81250,93750,3125,9375,15625,21875,28125,34375,40625,46875,53125,59375,65625,71875,78125,84375,90625,96875],"low":30000,"high":70000,"n":31}
⏱ Performance - must finish in 2000ms

n=31 balanced BST, O(n) pruning must complete within 2s.