Search⌘ K
AI Features

Solution: Delete Nodes And Return Forest

Explore how to remove given nodes from a binary tree to form a forest of disjoint subtrees. Understand the iterative depth-first search method to detach nodes, handle child nodes becoming new roots, and return all remaining tree roots after deletions.

Statement

Given the root of a binary tree where each node has a unique value, your task is to delete all nodes with values specified in the delete_nodes list. After performing the deletions, the tree will split into a forest—a collection of disjoint trees. Return the roots of the remaining trees in the forest in any order.

Constraints:

  • 00\leq nodes 100\leq100 ...