Search⌘ K
AI Features

Solution: Delete Nodes And Return Forest

Understand how to delete nodes specified in a list from a binary tree and return the resulting forest of disjoint trees. Explore a depth-first traversal approach that processes each node to efficiently handle deletions and track new tree roots. This lesson helps you implement and analyze a solution optimized for time and space complexity.

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