Python clean code

  • 0
    class Solution(object):
        def helper(self, node, mp):
            if not node:
                return 0
            l = self.helper(node.left, mp)
            r = self.helper(node.right, mp)
            maxValue = max(l + r + node.val, node.val+l, node.val+r, node.val)
            mp["maxValue"] = max(maxValue, mp["maxValue"])
            pathValue = max(l+node.val, r+node.val, node.val)
            return pathValue
        def maxPathSum(self, root):
            :type root: TreeNode
            :rtype: int
            # post-order, tree, bt
            mp = {"maxValue": -2*31}
            self.helper(root, mp)
            return mp["maxValue"]

Log in to reply

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