-
Notifications
You must be signed in to change notification settings - Fork 17
/
delete-nodes-and-return-forest.py
42 lines (31 loc) · 1.05 KB
/
delete-nodes-and-return-forest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from typing import Optional
# Definition for a binary tree node.
# 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]:
new_roots = []
to_delete_set = set(to_delete)
def dfs(node: Optional[TreeNode]) -> bool:
if not node:
return
if node.val in to_delete_set:
if node.left:
new_roots.append(node.left)
if node.right:
new_roots.append(node.right)
if dfs(node.left):
node.left = None
if dfs(node.right):
node.right = None
return node.val in to_delete_set
if not dfs(root):
new_roots.append(root)
result = []
for root in new_roots:
if root.val not in to_delete_set:
result.append(root)
return result