Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def delNodes(root: TreeNode, to_delete: List[int]) -> List[TreeNode]:
- delete = set(to_delete)
- forest = []
- def _prune(node, isRoot):
- if node is None:
- return
- if node.val in delete:
- _prune(node.left, True)
- _prune(node.right, True)
- else:
- if isRoot:
- forest.append(node)
- _prune(node.left, False)
- _prune(node.right, False)
- if node.left and node.left.val in delete:
- node.left = None
- if node.right and node.right.val in delete:
- node.right = None
- _prune(root, True)
- return forest
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement