Search⌘ K
AI Features

Solution: Delete Nodes And Return Forest

Explore how to delete nodes from a binary tree and return the collection of remaining trees as a forest. Understand the step-by-step iterative algorithm using depth-first search that processes each node, detaches specified nodes, and collects new tree roots. Learn to apply this approach efficiently, with time and space complexity considerations.

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 deleteNodes array. 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

  • 11\leq nodes.value 1000\leq 1000 ...