Delete Nodes and Return Forest

Problem Given the root of a binary tree with distinct values and a list to_delete, remove every node whose value is in to_delete. Deletions split the tree into a forest; return the roots of the remaining trees.

Input / Output

  • Input: root, int[] to_delete.
  • Output: list of forest roots (any order).

Constraints

  • Up to 1000 nodes, distinct values; O(n) expected.

Example

  • root = [1,2,3,4,5,6,7], to_delete = [3,5] → forests [[1,2,null,4],[6],[7]].
asked …
LeaderboardSalaryAccount