Solution Review: Trim the Tree Nodes Outside Range

Let’s take a detailed look at the previous challenge’s solution.

We'll cover the following

Solution

We traverse the tree and delete any node that is not in the given range. We compare the current node with min and max. If it is smaller than min, we don’t need to take care of its left subtree. If it is greater than max, we don’t need to take care of its right subtree.

All the deletion will happen from the inside out, so we do not have to care about the children of a node, because if they are out of range then they have already deleted themselves.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.