Python 6 lines

  • 0
        def maxPathSum(self, root):
            def maxPath(root):
                if not root: return 0, float("-inf")
                lPath, lMax = maxPath(root.left)
                rPath, rMax = maxPath(root.right)
                return max(lPath,rPath,0) + root.val, max(root.val + max(0,lPath) + max(0,rPath),lMax,rMax)
            return maxPath(root)[1]

Log in to reply

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