124. 二叉树中的最大路径和
解法一:递归遍历
class Solution:
def maxPathSum(self, root: TreeNode) -> int:
self.maxSum = -sys.maxsize
self.getMax(root)
return self.maxSum
def getMax(self, root):
if not root:
return 0
left = self.getMax(root.left)
right = self.getMax(root.right)
self.maxSum = max(self.maxSum, left+right+root.val)
return max(left, right) + root.val最后更新于