Back to problems

Delete Nodes and Return the Resulting Forest

Algorithm · Google · Medium

You are given the root of a binary tree together with a collection of values to_delete. Remove every node whose value appears in to_delete. Removing a node works as follows: Its children that exist are kept, not removed along with it. Each surviving child becomes the root of its own separate tree. If the original root is not removed, it stays as the root of one of the resulting trees and must be reported. After all removals you are left with a forest. Return the root values…

Loading the rest of the question…