class Solution(object): def maxPathSum(self, root): """ :type root: TreeNode :rtype: int """ self.maxSum = float('-inf') self._maxPathSum(root) return self.maxSum
def _maxPathSum(self, root): # DFS if root is None: return 0 left = self._maxPathSum(root.left) right = self._maxPathSum(root.right) left = left if left > 0 else 0 right = right if right > 0 else 0 self.maxSum = max(self.maxSum, root.val + left + right) # print self.maxSum return max(left, right) + root.val