1110. Delete Nodes And Return Forest
Last updated
Was this helpful?
Last updated
Was this helpful?
Given the root
of a binary tree, each node in the tree has a distinct value.
After deleting all nodes with a value in to_delete
, we are left with a forest (a disjoint union of trees).
Return the roots of the trees in the remaining forest. You may return the result in any order.
Example 1:
Constraints:
The number of nodes in the given tree is at most 1000
.
Each node has a distinct value between 1
and 1000
.
to_delete.length <= 1000
to_delete
contains distinct values between 1
and 1000
.
使用递归的方式,先把待delete的数组转换为set,以方便检测。如果是root而且不在delete set里,则加进最终list;继续执行左右child node;最后如果是delete的node,return null;如不是,return当前node。