-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeleteNodesAndReturnForest.py
98 lines (66 loc) · 2.85 KB
/
DeleteNodesAndReturnForest.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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:
forest: List[TreeNode] = []
def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]:
self.forest = []
to_delete_set = set(to_delete)
root = self._helper(root, to_delete_set)
if root:
self.forest.append(root)
return self.forest
def _helper(self, root: Optional[TreeNode], to_delete: List[int]):
if root is None:
return None
root.left = self._helper(root.left, to_delete)
root.right = self._helper(root.right, to_delete)
if root.val in to_delete:
if root.left:
self.forest.append(root.left)
if root.right:
self.forest.append(root.right)
return None # to delete the current node, return None to its parent
return root
# initial solution: works, but clunky code.
# checking for 2 levels down instead of 1 level down.
# bad readability
# class Solution:
# forest: List[TreeNode] = []
# def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]:
# self.forest = []
# to_delete_set = set(to_delete)
# if root.val in to_delete_set:
# to_delete_set.remove(root.val)
# if root.left is not None and root.left.val not in to_delete_set:
# self.forest.append(root.left)
# if root.right is not None and root.right.val not in to_delete_set:
# self.forest.append(root.right)
# else:
# self.forest.append(root)
# self.helper(root, to_delete_set)
# return self.forest
# def helper(self, root: Optional[TreeNode], to_delete: List[int]):
# if root is None or not to_delete:
# return
# if root.left is not None:
# self.helper(root.left, to_delete)
# if root.left.val in to_delete:
# to_delete.remove(root.left.val)
# if root.left.right is not None:
# self.forest.append(root.left.right)
# if root.left.left is not None:
# self.forest.append(root.left.left)
# root.left = None
# if root.right is not None:
# self.helper(root.right, to_delete)
# if root.right.val in to_delete:
# to_delete.remove(root.right.val)
# if root.right.right is not None:
# self.forest.append(root.right.right)
# if root.right.left is not None:
# self.forest.append(root.right.left)
# root.right = None