肥肥今天又翘班打code了
希望老板不要把我sa了
def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) ->
List[TreeNode]:
ans = []
if root.val not in to_delete:
ans.append(root)
def dfs(root):
nonlocal ans
if root is None:
return root
root.right = dfs(root.right)
root.left = dfs(root.left)
delete_cur = root.val in to_delete
if delete_cur:
if root.left:
ans.append(root.left)
if root.right:
ans.append(root.right)
return None if delete_cur else root
dfs(root)
return ans