Trim a binary search tree to keep only values within [low, high]

Quick Overview

Remove every node of a binary search tree whose value falls outside an inclusive range while keeping the remaining nodes in their original ancestor order, and return the new root. Tests use of the BST ordering to discard whole subtrees, handling a root that changes, and recursion depth on skewed trees.

Trim a binary search tree to keep only values within [low, high]

Company: Yahoo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given the `root` of a binary search tree and the lowest and highest boundaries as `low` and `high`, trim the tree so that all its elements lie in `[low, high]`. Trimming the tree should not change the relative structure of the elements that will remain in the tree (any node's descendant should remain a descendant). It can be proven that there is a unique answer. Return the `root` of the trimmed binary search tree. Note that the root may change depending on the given bounds. ### Function Signature ```python def trim_bst(root: list[int | None], low: int, high: int) -> list[int | None]: ``` ### Rules - Trees are given and returned as level-order lists. The first element is the root. Then, for each non-null node in the order it appears, the list gives its left child followed by its right child, with `None` for a missing child. Trailing `None` values are dropped. - If no value lies in `[low, high]`, return `[]`. - Both bounds are inclusive. ### Constraints - The number of nodes in the tree is in the range `[1, 10^4]`. - `0 <= Node.val <= 10^4` - The value of each node in the tree is unique. - `root` is guaranteed to be a valid binary search tree. - `0 <= low <= high <= 10^4` ### Examples ```text Input: root = [1, 0, 2], low = 1, high = 2 Output: [1, None, 2] Original tree: After trimming: 1 1 / \ \ 0 2 2 ``` Node 0 is below `low`, so it is removed and 1 keeps only its right child. ```text Input: root = [3, 0, 4, None, 2, None, None, 1], low = 1, high = 3 Output: [3, 2, None, 1] Original tree: After trimming: 3 3 / \ / 0 4 2 \ / 2 1 / 1 ``` Node 4 is above `high` and node 0 is below `low`. The subtree under 0 still holds 2 and 1, which are in range, so 2 takes the place of 0 as the left child of 3. ```text Input: root = [5, 3, 8, 2, 4, 7, 9], low = 6, high = 8 Output: [8, 7] Original tree: After trimming: 5 8 / \ / 3 8 7 / \ / \ 2 4 7 9 ``` The root 5 and its whole left subtree are below 6, so the trimmed tree starts at 8. Node 9 is above 8 and is removed.

Overview: Remove every node of a binary search tree whose value falls outside an inclusive range while keeping the remaining nodes in their original ancestor order, and return the new root. Tests use of the BST ordering to discard whole subtrees, handling a root that changes, and recursion depth on skewed trees.

Yahoo logo
Yahoo
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given the root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all its elements lie in [low, high]. Trimming the tree should not change the relative structure of the elements that will remain in the tree (any node's descendant should remain a descendant). It can be proven that there is a unique answer.

Return the root of the trimmed binary search tree. Note that the root may change depending on the given bounds.

Function Signature

def trim_bst(root: list[int | None], low: int, high: int) -> list[int | None]:

Rules

  • Trees are given and returned as level-order lists. The first element is the root. Then, for each non-null node in the order it appears, the list gives its left child followed by its right child, with None for a missing child. Trailing None values are dropped.
  • If no value lies in [low, high] , return [] .
  • Both bounds are inclusive.

Constraints

  • The number of nodes in the tree is in the range [1, 10^4] .
  • 0 <= Node.val <= 10^4
  • The value of each node in the tree is unique.
  • root is guaranteed to be a valid binary search tree.
  • 0 <= low <= high <= 10^4

Examples

Input: root = [1, 0, 2], low = 1, high = 2
Output: [1, None, 2]

Original tree:      After trimming:
    1                   1
   / \                   \
  0   2                   2

Node 0 is below low, so it is removed and 1 keeps only its right child.

Input: root = [3, 0, 4, None, 2, None, None, 1], low = 1, high = 3
Output: [3, 2, None, 1]

Original tree:      After trimming:
      3                   3
     / \                 /
    0   4               2
     \                 /
      2               1
     /
    1

Node 4 is above high and node 0 is below low. The subtree under 0 still holds 2 and 1, which are in range, so 2 takes the place of 0 as the left child of 3.

Input: root = [5, 3, 8, 2, 4, 7, 9], low = 6, high = 8
Output: [8, 7]

Original tree:      After trimming:
      5                   8
    /   \                /
   3     8              7
  / \   / \
 2   4 7   9

The root 5 and its whole left subtree are below 6, so the trimmed tree starts at 8. Node 9 is above 8 and is removed.