Skip to content

Commit 5c091aa

Browse files
committed
Algorithm to get maximum path sum of a binary tree.
1 parent e798e5a commit 5c091aa

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
"""
2+
A path in a binary tree is a sequence of nodes where each pair
3+
of adjacent nodes in the sequence has an edge connecting
4+
them. A node can only appear in the sequence at most once. Note
5+
that the path does not need to pass through the root.
6+
7+
The path sum of a path is the sum of the node's values in the path.
8+
9+
Given the root of a binary tree, return the maximum path sum of any non-empty path.
10+
11+
Leetcode Reference : https://leetcode.com/problems/binary-tree-maximum-path-sum/
12+
"""
13+
14+
15+
class TreeNode:
16+
17+
"""
18+
TreeNode has tree variables, val -> Stores value of the node
19+
left, right -> Stores the pointer to left or right node.
20+
"""
21+
22+
def __init__(self, val: int, left=None, right=None) -> None:
23+
self.val: int = val
24+
self.left: TreeNode | None = left
25+
self.right: TreeNode | None = right
26+
27+
28+
class GetMaxPathSum:
29+
30+
r"""
31+
32+
GetMaxPathSum takes root node of a tree as initial argument.
33+
Upon calling max_path_sum(), it returns maximum path
34+
sum from the tree.
35+
36+
# Test
37+
38+
The below tree looks like this
39+
10
40+
/ \
41+
5 -3
42+
/ \ \
43+
3 2 11
44+
/ \ \
45+
3 -2 1
46+
47+
Result will be calculated like : 3 -> 3 -> 5 -> 10 -> -3 -> 11
48+
As it is the maximum path possible.
49+
50+
51+
>>> root = TreeNode(10)
52+
>>> root.left = TreeNode(5)
53+
>>> root.right = TreeNode(-3)
54+
>>> root.left.left = TreeNode(3)
55+
>>> root.left.right = TreeNode(2)
56+
>>> root.right.right = TreeNode(11)
57+
>>> root.left.left.left = TreeNode(3)
58+
>>> root.left.left.right = TreeNode(-2)
59+
>>> root.left.right.right = TreeNode(1)
60+
61+
>>> GetMaxPathSum(root).max_path_sum()
62+
29
63+
"""
64+
65+
def __init__(self, root):
66+
self.sum = -9999999999
67+
self.root = root
68+
69+
def traverse(self, root: TreeNode) -> int:
70+
71+
"""
72+
Returns maximum path sum by recursively taking max_path_sum from left
73+
and max_path_sum from right if current Node has a left or right Node.
74+
75+
:param root -> tree root:
76+
:return int:
77+
"""
78+
79+
if root is None:
80+
return 0
81+
82+
right_sum = max(self.traverse(root.right), 0)
83+
left_sum = max(self.traverse(root.left), 0)
84+
85+
val = root.val + right_sum + left_sum
86+
self.sum = max(val, self.sum)
87+
88+
return root.val + max(right_sum, left_sum)
89+
90+
def max_path_sum(self) -> int:
91+
92+
"""
93+
Driver method to get max_path_sum by calling traverse method.
94+
:return max_path_sum:
95+
"""
96+
self.traverse(self.root)
97+
return self.sum
98+
99+
100+
def construct_tree() -> TreeNode:
101+
"""
102+
The below tree
103+
-10
104+
/ \
105+
9 20
106+
/ \
107+
15 7
108+
"""
109+
110+
root = TreeNode(-10)
111+
root.left = TreeNode(9)
112+
root.right = TreeNode(20)
113+
root.right.left = TreeNode(15)
114+
root.right.right = TreeNode(7)
115+
return root
116+
117+
118+
if __name__ == '__main__':
119+
import doctest
120+
121+
tree = GetMaxPathSum(construct_tree())
122+
max_sum = tree.max_path_sum()
123+
124+
print("Given example output: ", max_sum)
125+
126+
doctest.testmod()

0 commit comments

Comments
 (0)