Given the root of a binary search tree with distinct values and two integers lo <= hi, remove every node whose value is outside the range [lo, hi]. The remaining nodes must keep their relative order and the result must still be a search tree with the same ancestor relationships among the kept nodes. Return the root (None if nothing is left).
Examples
20 20
/ \ / \
11 31 15 25
/ \ / \ ==> / \
4 15 25 40 13 28
/ \
13 28
Input: root = build_tree([20, 11, 31, 4, 15, 25, 40, None, None, 13, None, None, 28]), lo = 12, hi = 30
Output: tree_to_list(...) == [20, 15, 25, 13, None, None, 28]
Input: root = build_tree([2, 1, 4, None, None, 3]), lo = 3, hi = 9
Output: [4, 3]
Explanation: the root 2 is too small, so the trimmed right subtree takes its place.
Input: root = build_tree([20, 11, 31, 4, 15, 25, 40, None, None, 13, None, None, 28]), lo = 11, hi = 20
Output: [20, 11, None, None, 15, 13]
Explanation: both ends of the range are kept, so 11 and 20 stay.
Constraints
0 <= number of nodes <= 1000-10**4 <= lo <= hi <= 10**4
Goals
- Use the ordering property to discard whole subtrees without visiting them
- Return the replacement subtree from each call