forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_117.py
61 lines (46 loc) · 1.11 KB
/
problem_117.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
import sys
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def __repr__(self):
return "{}=(l={}, r={})".format(self.val, self.left, self.right)
def get_maxsum_level_helper(level, nodes, parent_sum):
child_nodes = list()
nodes_sum = 0
for node in nodes:
nodes_sum += node.val
if node.left:
child_nodes.append(node.left)
if node.right:
child_nodes.append(node.right)
max_sum = max(nodes_sum, parent_sum)
if child_nodes:
max_sum = get_maxsum_level_helper(level + 1, child_nodes, max_sum)
return max_sum
def get_maxsum_level(root):
max_sum = get_maxsum_level_helper(0, [root], -sys.maxsize)
return max_sum
a = Node(1)
b = Node(2)
c = Node(3)
a.left = b
a.right = c
d = Node(4)
e = Node(5)
b.left = d
b.right = e
f = Node(6)
g = Node(7)
c.left = f
c.right = g
h = Node(8)
d.right = h
assert get_maxsum_level(a) == 22
a.val = 100
assert get_maxsum_level(a) == 100
b.val = 150
assert get_maxsum_level(a) == 153
h.val = 200
assert get_maxsum_level(a) == 200