#1110

Delete Nodes And Return Forest

pupil · 525 · lc medium +28 · failed · 72.5% accepted · 4,793 likes · top 82%

Description

Given the root of a binary tree (all node values distinct) and an array to_delete, delete all nodes whose values appear in to_delete. The deletion splits the tree into a forest.

Return the roots of all remaining trees in the forest (any order).

Example 1:

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

Example 2:

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

Code

1
2
3
4
5
6
7
8
9