LeetCode 124. Binary Tree Maximum Path Sum

Jene Hojin Choi·2021년 12월 21일
post-thumbnail

 def maxPathSum(self, root: Optional[TreeNode]) -> int:
        def helper(root):
            if not root:
                return 0
            left = max(0, helper(root.left))
            right = max(0, helper(root.right))
            res[0] = max(res[0], root.val + left + right)
            return root.val + max(left, right)
        
        res = [-float('inf')]
        helper(root)
        
        return res[0]

0개의 댓글