You are given a binary tree root whose node values are distinct, and an array to_delete. Remove every tree node whose value appears in to_delete, along with every edge connected to a removed node.
The surviving nodes form a forest: zero or more separate binary trees. Return a list containing the root of each remaining tree, in any order. Preserve the left and right child relationships between surviving nodes.
Examples represent each tree in level order, with null for missing children and trailing nulls omitted. An empty forest is [].
Filled nodes in the input diagram are deleted; the diagrams below the arrow show the resulting forest.
Example 1:
Input: root = [1,2,3,4,5]
to_delete = [2]
Output: [[1,null,3],[4],[5]]Explanation: Removing node 2 disconnects its children 4 and 5 from the original root. The remaining trees have roots 1, 4 and 5; root 1 still has right child 3.
Example 2:
Input: root = [2,1,3]
to_delete = [2]
Output: [[1],[3]]Explanation: Deleting the original root 2 leaves its left child 1 and right child 3 as two separate one-node trees. Either ordering of these roots is valid.
Example 3:
Input: root = [4]
to_delete = [4]
Output: []Explanation: The only node is deleted, so no trees remain and the result is an empty list.
Constraints:
- The tree contains at most
1000nodes. - Tree node values are distinct integers between
1and1000. to_delete.length <= 1000- Values in
to_deleteare distinct integers between1and1000.