forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol.py
57 lines (47 loc) · 1.69 KB
/
sol.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
from tree import Node
import random
class TreeNode(Node):
def __init__(self, val=None):
super().__init__(data=val)
def insert_node(self, val):
if self.left is None and self.right is not None:
self.left = TreeNode(val)
elif self.right is None and self.left is not None:
self.right = TreeNode(val)
elif self.left is None and self.right is None:
choice = random.choice(['LEFT', 'RIGHT'])
if choice=='LEFT':
self.left = TreeNode(val)
else:
self.right = TreeNode(val)
elif self.left is not None and self.right is not None:
choice = random.choice(['LEFT', 'RIGHT'])
if choice=='LEFT':
self.left.insert_node(val)
else:
self.right.insert_node(val)
def max_path_sum_util(root, res):
if root is None:
return 0
if root.left is None and root.right is None:
return root.data
l_sum = max_path_sum_util(root.left, res)
r_sum = max_path_sum_util(root.right, res)
if root.left is not None and root.right is not None:
res[0] = max(res[0], l_sum + r_sum + root.data)
return max(l_sum, r_sum) + root.data
elif root.left is not None and root.right is None:
return l_sum + root.data
elif root.left is None and root.right is not None:
return r_sum + root.data
def maxPathSum(root):
res = [-10**10]
max_path_sum_util(root, res)
return res[0]
if __name__=='__main__':
tree = TreeNode(50)
for _ in range(5):
val = random.randint(-50, 50)
tree.insert_node(val)
tree.prettyPrint(); print('\n\n')
print(maxPathSum(tree))