Stupid Python solution


  • 0
    S
    class Solution(object):
        def maxPathSum(self, root):#To calculate the maximum sum along the leaf to current node 
            self.res = []
            def maxoftree(node):
                if not node:
                    return 0
                left, right = maxoftree(node.left), maxoftree(node.right)
                self.res.append(max(node.val, node.val+left, node.val+right, node.val+right+left))#compare the current node value and n+left and n+right and n+left+right
                return max(node.val, node.val+left, node.val+right)
            maxoftree(root)
            return max(self.res)

Log in to reply
 

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