The problem. Given the root of a binary search tree and two numbers low and high, remove every node whose value is outside [low, high], keeping the relative structure of the rest. Return the root of the trimmed tree (it may be a different node).
root = [1, 0, 2], low = 1, high = 2 -> [1, null, 2]
root = [3, 0, 4, null, 2, null, null, 1], low = 1, high = 3 -> [3, 2, null, 1]
root = [5, 3, 8], low = 6, high = 9 -> [8]Members only · free account
The rest of this lesson, with 6 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson