Trim a Binary Search Tree - Problem

Given the root of a binary search tree and two boundary values low and high, your task is to trim the tree so that all remaining nodes have values within the range [low, high] (inclusive).

The key challenge is that trimming must preserve the BST structure - any node's descendants must remain as descendants in the trimmed tree. This means you can't simply remove nodes arbitrarily; you need to carefully reconstruct the tree while maintaining BST properties.

Important: The root of the trimmed tree might be different from the original root, depending on whether the original root's value falls within the given bounds.

Example: If you have a BST with nodes [1, 0, 2] and bounds [1, 2], the trimmed tree should only contain nodes with values 1 and 2, maintaining their parent-child relationships.

Input & Output

example_1.py — Basic Trimming
$ Input: root = [1,0,2], low = 1, high = 2
Output: [1,null,2]
💡 Note: Node 0 is outside the range [1,2], so it gets removed. The result is a tree with root 1 and right child 2.
example_2.py — Complex Trimming
$ Input: root = [3,0,4,null,2,null,null,1], low = 1, high = 3
Output: [3,2,null,1]
💡 Note: Nodes 0 and 4 are outside [1,3]. After trimming, node 2 becomes the left child of 3, and node 1 becomes the left child of 2.
example_3.py — Edge Case - Single Node
$ Input: root = [1], low = 1, high = 2
Output: [1]
💡 Note: Single node with value 1 is within range [1,2], so no trimming needed.

Visualization

Tap to expand
🌳 Garden Tree Pruning AnalogyKeep fruits size 1-3Before Pruning30421Too small!Too big!After Smart Pruning321✨ Perfect!
Understanding the Visualization
1
Inspect Current Branch
Look at the current branch's fruit size
2
Make Smart Decision
If too small, only check right side (larger fruits). If too large, only check left side (smaller fruits)
3
Keep Good Branches
If the fruit is just right, keep this branch and check both sides for more good fruits
4
Natural Structure
The tree maintains its natural organization throughout the pruning process
Key Takeaway
🎯 Key Insight: BST properties let us make smart pruning decisions - we can eliminate entire subtrees without checking every branch, just like knowing that all fruits on the left are smaller than the current branch!

Time & Space Complexity

Time Complexity
⏱️
O(n)

In worst case, we visit each node exactly once to determine if it should be kept or discarded

n
2n
Linear Growth
Space Complexity
O(h)

Space used by recursion stack, where h is height of tree. O(log n) for balanced tree, O(n) for skewed tree

n
2n
Linear Space

Constraints

  • The number of nodes in the tree is in the range [1, 104]
  • 0 ≤ Node.val ≤ 104
  • The value of each node in the tree is unique
  • root is guaranteed to be a valid binary search tree
  • 0 ≤ low ≤ high ≤ 104
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
68.0K Views
Medium-High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen