Skip to content

Commit c34b649

Browse files
Solve : Binary Tree Maximum Path Sum
1 parent 6f38c76 commit c34b649

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maxPathSum(self, root):
3+
self.max_sum = float('-inf')
4+
def dfs(node):
5+
if not node:
6+
return 0
7+
left = max(dfs(node.left), 0)
8+
right = max(dfs(node.right), 0)
9+
self.max_sum = max(self.max_sum, node.val + left + right)
10+
return node.val + max(left, right)
11+
dfs(root)
12+
return self.max_sum

0 commit comments

Comments
 (0)