124 Binary Tree Maximum Path Sum

题目: 124 Binary Tree Maximum Path Sum

这道题就是分别算出左子树和右子树的可能最大和,然后对Path的值进行更新即可

class Solution:
    def __init__(self):
        self.ans = -100000

    def maxPathSum(self, root):
        self.getSum(root)
        return self.ans

    def getSum(self, root):
        if not root:
            return 0
        left = max(0, self.getSum(root.left))
        right = max(0, self.getSum(root.right))
        self.ans = max(self.ans, left + root.val + right)
        return root.val + max(left,right)

 

posted @ 2015-07-15 12:40  dapanshe  阅读(106)  评论(0编辑  收藏  举报