-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree_and_binary_search.py
More file actions
165 lines (118 loc) · 3.12 KB
/
binary_tree_and_binary_search.py
File metadata and controls
165 lines (118 loc) · 3.12 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from typing import List, Optional, Self, Union
class TreeNode:
def __init__(self, val: int = 0, left: Union[Self, None] = None, right: Union[Self, None] = None):
self.val = val
self.left = left
self.right = right
def search(nums: List[int], target: int) -> int:
pos = len(nums) // 2
offset = 0
while nums:
number = nums[pos]
if number == target:
return offset + pos
if target > number:
nums = nums[pos+1:]
offset += pos + 1
else:
nums = nums[:pos]
pos = len(nums) // 2
return -1
def firstBadVersion(n: int) -> int:
#mock
def isBadVersion(version: int) -> bool:
pass
offset = 0
bad_version = 0
while n > 0:
current = n // 2
version = offset + current + 1
if not isBadVersion(version):
steps_to_move = current + 1
n -= steps_to_move
offset += steps_to_move
else:
n = current
bad_version = version
return bad_version
def sortedArrayToBST(nums: List[int]) -> Optional[TreeNode]:
n = len(nums)
if n == 0:
return None
middle = n // 2
head = TreeNode(nums[middle], nums[:middle], nums[middle+1:])
def createNodes(current: TreeNode):
if not current.left:
current.left = None
if not current.right:
current.right = None
if current.left:
left_i = len(current.left) // 2
current.left = TreeNode(
current.left[left_i],
current.left[:left_i],
current.left[left_i+1:]
)
createNodes(current.left)
if current.right:
right_i = len(current.right) // 2
current.right = TreeNode(
current.right[right_i],
current.right[:right_i],
current.right[right_i+1:]
)
createNodes(current.right)
createNodes(head)
return head
def sortedArrayToBST_V2(nums: List[int]) -> Optional[TreeNode]:
n = len(nums)
if n == 0:
return None
middle = n // 2
node = TreeNode(nums[middle])
node.left = sortedArrayToBST_V2(nums[:middle])
node.right = sortedArrayToBST_V2(nums[middle+1:])
return node
def isBalanced(root: Optional[TreeNode]) -> bool:
if not root:
return True
def check(node: TreeNode):
dl = dr = 0
ok_l = ok_r = True
if node.left:
ok_l, dl = check(node.left)
if node.right:
ok_r, dr = check(node.right)
ok = abs(dl - dr) <= 1 and ok_l and ok_r
depth = 1 + max(dl, dr)
return ok, depth
return check(root)[0]
#não funciona pra solução ótima porque não permite skippar mais de um nó por vez
def rob(root: Optional[TreeNode]) -> int:
if not root:
return 0
def walk(node: TreeNode, get: bool) -> int:
total = 0
if node and get:
total += node.val
if node.left:
total += walk(node.left, not get)
if node.right:
total += walk(node.right, not get)
return total
sum_1 = walk(root, True)
sum_2 = walk(root, False)
return max(sum_1, sum_2)
#solução correta com solução ótima
def robV2(root: Optional[TreeNode]) -> int:
if not root:
return 0
def dfs(node: TreeNode):
if not node:
return 0, 0
robbing_left, skipping_left = dfs(node.left)
robbing_right, skipping_right = dfs(node.right)
robbing = node.val + skipping_left + skipping_right
skipping = max(robbing_left, skipping_left) + max(robbing_right, skipping_right)
return robbing, skipping
return max(dfs(root))