一、leetcode地址
https://leetcode.com/problems/binary-tree-maximum-path-sum/
二、问题描述
三、代码实现
语言:Python3
代码:
class Solution:
def maxPathSum(self, root: TreeNode) -> int:
max_ = float('-inf')
def max_sum(root):
nonlocal max_
if not root:
return 0
l_max = max(max_sum(root.left),0)
r_max = max(max_sum(root.right),0)
temp = root.val + l_max + r_max
if temp > max_:max_ = temp
return root.val+max(l_max,r_max)
max_sum(root)
return max_