-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1110 Daily.py
42 lines (33 loc) · 1.31 KB
/
m1110 Daily.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
# 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: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]:
output = []
to_delete = set(to_delete)
# Returns pointer --> if remove then return null
def dfs(curr: Optional[TreeNode], to_delete: set, output: List[Optional[TreeNode]]) -> Optional[TreeNode] :
if not curr :
return None
# If found quickly, rest of traversal is redundant
if not to_delete :
return curr
if curr.val in to_delete :
to_delete.remove(curr.val)
left = dfs(curr.left, to_delete, output)
right = dfs(curr.right, to_delete, output)
if left :
output.append(left)
if right :
output.append(right)
return None
curr.left = dfs(curr.left, to_delete, output)
curr.right = dfs(curr.right, to_delete, output)
return curr
head = dfs(root, to_delete, output)
if head :
output.append(head)
return output