Skip to content
Problem 669 · LeetCode 669(opens in a new tab)

Trim a Binary Search Tree

MediumBinary search trees

You get the root of a binary search tree with distinct values, and two bounds low and high. Remove every node whose value is outside [low, high].

The nodes that stay must keep their family ties: if one kept node was below another in the input, it is still below it in the result. There is exactly one tree that does this. Return its root, which may not be the original root, or null if no node is left.

Example 1

Input:  root = [5, 2, 8, 1, 4, 6, 9], low = 3, high = 7
Output: [5, 4, 6]
Why:    1, 2, 8 and 9 go; 4 takes the place of 2, 6 the place of 8
2 more examples and the constraints

Example 2

Input:  root = [6, 2, 9, 1, 4, 8, 10], low = 7, high = 10
Output: [9, 8, 10]
Why:    6 and its left side are too small, so 9 becomes the root

Example 3

Input:  root = [4, 2, 6, null, 3], low = 2, high = 3
Output: [2, null, 3]
Why:    4 and 6 are too big; 2 keeps its right child 3

Constraints

  • The tree has between 1 and 10,000 nodes
  • Every value is distinct and between 0 and 10,000
  • The input is a correct BST
  • The bounds satisfy 0 ≤ low ≤ high ≤ 10,000

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Recursive trimO(n)
  2. Fix the root, then the two edgesO(h)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeNodes visited, 10,000-node balanced tree, wide rangeVerdict
Recursive trimO(n)
Passes; short to write
Fix the root, then the two edgesO(h)
Best; touches two paths

n = nodes in the tree, h = its height.

Edge cases to test

  • Nothing to cut. With low = 0 and high = 10,000, the tree comes back unchanged.
  • Everything is cut. If no value lies in [low, high], return null, printed as [].
  • The root goes. In Example 2 the root 6 is too small and 9 becomes the new root.
  • A one-value range. low = high leaves at most one node.
  • A tree shaped like a line. 10,000 nodes in a chain make recursion 10,000 calls deep.

Common mistakes

  • Returning null for an out-of-range node, which also throws away the in-range nodes on its other side.
  • Checking only the direct children of the root instead of walking all the way down each edge.
  • Forgetting that the root itself can be out of range, so the answer starts somewhere else.

How to talk through it

  1. 1Clarify. Are the bounds inclusive? Can the result be empty? Is it fine to change the tree in place?
  2. 2Use the order. A node below low means its whole left side is below low too.
  3. 3Write the recursion. Out of range: return the trimmed other side. In range: trim both children.
  4. 4Tighten it. Only the two edges below the new root can hold bad nodes, so walk just those: O(h), no stack.
  5. 5Test out loud. Run a range that keeps everything, one that keeps nothing, and one that removes the root.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?