Simple python implement

  • 0
    class Solution(object):
        def maxPathSum(self, root):
            max_v, _ = self.postorder(root)
            return max_v
        def postorder(self, root):
            if root is None: return -0x80000000, 0
            left_max, left = self.postorder(root.left)
            right_max, right = self.postorder(root.right)
            root_max = (left if left>0 else 0) + (right if right>0 else 0) + root.val
            max_v = max([root_max, left_max, right_max])
            return max_v, root.val + max([0, left, right])

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.