-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path94_Google_Max_Path_Two_Nodes_of_Binary_Tree.py
executable file
·123 lines (103 loc) · 4.36 KB
/
94_Google_Max_Path_Two_Nodes_of_Binary_Tree.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
This problem was asked by Google.
Given a binary tree of integers, find the maximum path sum between two nodes.
The path must go through at least one node, and does not need to go through the root.
"""
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.right = right
self.left = left
def __repr__(self):
if self.right is not None:
fmt = '{}({value!r}, {left!r}, {right!r})'
elif self.left is not None:
fmt = '{}({value!r}, {left!r})'
else:
fmt = '{}({value!r})'
return fmt.format(type(self).__name__, **vars(self))
def find_max_path_sum(root : Node):
max_sum = float('-inf')
def helper(root):
nonlocal max_sum
left_value, right_value = None, None
if root.left:
left_value = helper(root.left)
if root.right:
right_value = helper(root.right)
if right_value and left_value:
max_sum = max(max_sum, left_value+right_value+root.value)
return max(left_value+root.value, right_value+root.value, root.value)
elif left_value:
max_sum = max(max_sum, left_value + root.value)
return max(left_value+root.value, root.value)
elif right_value:
max_sum = max(max_sum, right_value + root.value)
return max(right_value + root.value, root.value)
return root.value # if leaf
helper(root)
return max_sum
if __name__ == '__main__':
"""
1
/ \ max = 6
2 3
"""
tree_1 = Node(1, left=Node(2), right=Node(3))
print(find_max_path_sum(tree_1))
print("\n-------------\n")
"""
-10
/ \ max=42 20
9 20 / \
/ \ 15 7
15 7
"""
tree_2 = Node(-10,
left=Node(9),
right=Node(20,
left=Node(15),
right=Node(7)))
print(find_max_path_sum(tree_2))
print("\n-------------\n")
"""
-15 6
/ \ / \
5 6 3 9
/ \ / \ \
-8 1 3 9 max = 27 0
/ \ \ \
2 6 0 -1
/ \ /
4 -1 10
/
10
"""
tree_3 = Node(-15,
left=Node(5,
left=Node(-8, left=Node(2), right=Node(6)),
right=Node(1)
),
right=Node(6,
left=Node(3),
right=Node(9,
right=Node(0, left=Node(4),
right=Node(-1, left=Node(10)))))
)
print(find_max_path_sum(tree_3))
print("\n-------------\n")
"""
-10
/ \ max=42 20
9 20 / \
/ \ 15 7
15 7
/ \
-15 -7
"""
tree_4 = Node(-10,
left=Node(9),
right=Node(20,
left=Node(15, left=Node(-15)),
right=Node(7, right=Node(-7))))
print(find_max_path_sum(tree_4))