# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def delNodes(self, root: TreeNode, to_delete: List[int]) -> List[TreeNode]:
#TC: O(nodes)
#SC: O(max depth)
#trick to use RETURN and FLAG
to_delete = set(to_delete)
res = []
def help(root, isRoot):
if not root:
return None
if root.val not in to_delete:
if isRoot:
res.append(root)
root.left = help(root.left, False)
root.right = help(root.right, False)
return root
else:
help(root.left, True)
help(root.right, True)
return None
help(root, True)
return res
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla