DSA Factory
Free Binary search trees lessonsBinary search trees · Stage 1 · Ordered walks · Step 3

Cut a tree down to a range

Remove every node whose value lies outside a given range from a search tree, keeping the others in place, by discarding whole sides when a node is out of range. About 14 minutes.

Out of range means skip a side

Suppose the allowed range is from low to high. If the current node is smaller than low, it must go. And since everything on its left is even smaller, that whole side must go too. The only thing that may survive is the right subtree, after trimming.

So replace the node by the trimmed version of its right subtree. A node bigger than high is handled as a mirror image, using its left subtree.

Below low: answer is the trimmed right side.
Above high: answer is the trimmed left side.
In code
if root.val < low:
    return trim_bst(root.right, low, high)
if root.val > high:
    return trim_bst(root.left, low, high)
Keep only the values from 2 to 5 in the tree 5, 3, 6, 2, 4, none, 7.
234567
range
2 to 5

5 is in range. Keep it and trim both sides.

Move 1 of 3

In range means keep it

A node inside the range stays. Its left and right children may still need trimming, so replace each child by the trimmed version of itself, and return the node.

An empty tree stays empty. Because each call returns the new top of its subtree, a parent just stores whatever its child call returned, and dropped nodes simply lose their links.

In range: keep the node, trim both sides.
Store the result: of each call as the new child.
In code
root.left = trim_bst(root.left, low, high)
root.right = trim_bst(root.right, low, high)
return root
Quick check

A node is smaller than the lowest allowed value. Where can survivors be?

  1. AOnly in its right subtree
  2. BOnly in its left subtree
  3. CIn both subtrees
Show the answer

Only in its right subtree. Everything in its left subtree is smaller still.

Your problem

Trim a search tree

Given the root of a binary search tree and two integers low and high, trim the tree so that all of its values are in the range from low to high, both included. Trimming must not change the relative structure of the nodes that stay: a node that stays keeps its descendants that stay. Return the root of the trimmed tree, which may be a different node from the original root.

Example
root = [5, 3, 6, 2, 4, null, 7], low = 2, high = 5 → [5, 3, null, 2, 4]

0 ≤ number of nodes ≤ 10,000 · 0 ≤ node.val ≤ 10,000 · low ≤ high

Solve it in your browserFree account, no card. Python, C++, Java or JavaScript, with hints if you get stuck, and your progress is saved.
Sign up free to solve