Delete Nodes And Return Forest

Try to solve the Delete Nodes And Return Forest problem.

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

  • 0≤0\leq nodes ≤100\leq100

  • 1≤1\leq nodes.value ≤1000\leq 1000

  • 0≤0\leq deleteNodes.length ≤100\leq 100

  • 1≤1\leq deleteNodes[i] ≤1000\leq 1000

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

Examples

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.