We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6f38c76 commit c34b649Copy full SHA for c34b649
binary-tree-maximum-path-sum/printjin-gmailcom.py
@@ -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