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
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