-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path1110.py
33 lines (29 loc) · 1.16 KB
/
1110.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
__________________________________________________________________________________________________
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def dfs(self, root: TreeNode, detached: bool):
if not root:
return
if detached and root.val not in self.x:
self.ans.append(root)
detached = (root.val in self.x)
self.dfs(root.left, detached)
self.dfs(root.right, detached)
if root.left and root.left.val in self.x:
root.left = None
if root.right and root.right.val in self.x:
root.right = None
if root.val in self.x:
del root
def delNodes(self, root: TreeNode, to_delete: List[int]) -> List[TreeNode]:
self.x = set(to_delete)
self.ans = []
self.dfs(root, detached=True)
return self.ans
__________________________________________________________________________________________________
__________________________________________________________________________________________________