Problem
Ask
Submissions

Problem: Delete Nodes And Return Forest

Medium
30 min
Explore how to delete nodes from a binary tree based on given values, causing the tree to split into a forest of smaller trees. Learn to return the roots of these trees using depth-first search, helping you solve related interview problems involving tree structures.

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

  • 11\leq nodes.value 1000\leq 1000

  • 00\leq delete_nodes.length 100\leq 100

  • 11\leq delete_nodes[i] 1000\leq 1000

Note: Both nodes and delete_nodes[i] will have distinct values.

Problem
Ask
Submissions

Problem: Delete Nodes And Return Forest

Medium
30 min
Explore how to delete nodes from a binary tree based on given values, causing the tree to split into a forest of smaller trees. Learn to return the roots of these trees using depth-first search, helping you solve related interview problems involving tree structures.

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

  • 11\leq nodes.value 1000\leq 1000

  • 00\leq delete_nodes.length 100\leq 100

  • 11\leq delete_nodes[i] 1000\leq 1000

Note: Both nodes and delete_nodes[i] will have distinct values.