【題目描述】
【原始碼】class Solution(object):
def maxPathSum(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.res = float('-inf')
self.dfs(root)
return self.res
def dfs(self, root):
if not root:
return 0
#print '執行dfs(',root.val,')'
left = max(0, self.dfs(root.left))
right = max(0, self.dfs(root.right))
#print 'self.res',left + root.val + right
self.res = max(self.res, left + root.val + right)
#print '返回給上層的是',root.val + max(left, right)
return root.val + max(left, right)
複製程式碼