Tap here to switch tabs
Problem
Submissions

Problem: Trim a Binary Search Tree

Trim a Binary Search Tree by removing nodes whose values fall outside the inclusive range from low to high. Return the remaining tree while preserving ancestor relationships, even if the root changes.

Statement

Given the root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all of its elements lie within the inclusive range [[low$, ‘high‘`high`]$.

Trimming the tree should preserve the relative structure of the remaining nodes, such that any node that was a descendant of another node remains a descendant. Return the root of the trimmed binary search tree.

Note: The root of the tree may change depending on the given bounds.

Constraints:

  • The number of nodes in the tree is in the range [1,104][1, 10^4].

  • 0≤0 \leq Node.val ≤104\leq 10^4

  • The value of each node in the tree is unique.

  • root is guaranteed to be a valid binary search tree.

  • 0≤0 \leq low ≤\leq high ≤104\leq 10^4

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Trim a Binary Search Tree

Trim a Binary Search Tree by removing nodes whose values fall outside the inclusive range from low to high. Return the remaining tree while preserving ancestor relationships, even if the root changes.

Statement

Given the root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all of its elements lie within the inclusive range [[low$, ‘high‘`high`]$.

Trimming the tree should preserve the relative structure of the remaining nodes, such that any node that was a descendant of another node remains a descendant. Return the root of the trimmed binary search tree.

Note: The root of the tree may change depending on the given bounds.

Constraints:

  • The number of nodes in the tree is in the range [1,104][1, 10^4].

  • 0≤0 \leq Node.val ≤104\leq 10^4

  • The value of each node in the tree is unique.

  • root is guaranteed to be a valid binary search tree.

  • 0≤0 \leq low ≤\leq high ≤104\leq 10^4