Delete Nodes And Return Forest

Medium

Description

Given the root of a binary tree and array of values to delete, delete nodes with those values. Return the roots of the remaining trees.

Examples

Input:root = [1,2,3,4,5,6,7], to_delete = [3,5]
Output:[[1,2,null,4],[6],[7]]
Explanation:

Remaining forest.

Input:root = [1], to_delete = [1]
Output:[1]
Explanation:

Edge case with a single-element array.

Input:root = [1,2,3,null,4,5,6], to_delete = [2,5]
Output:[[1,null,3,null,null,null,6],[4]]
Explanation:

When node 2 is deleted, its children (node 4) become separate trees. When node 5 is deleted from the right subtree, node 6 remains attached to node 3. The result is two trees: the main tree with root 1 (missing the left child 2 and node 5), and a single-node tree with root 4.

Constraints

  • 1 ≤ nodes ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!