straightforward python solution


  • 0
    def hasPathSum(self, root, sum):
            """
            :type root: TreeNode
            :type sum: int
            :rtype: bool
            """
            if not root:
                return False
            if not root.left and not root.right:
                return sum - root.val == 0
            if root.left:
                left = self.hasPathSum(root.left, sum - root.val)
                if left:
                    return True
            if root.right:
                right = self.hasPathSum(root.right, sum - root.val)
                if right:
                    return True
            return False
    

Log in to reply
 

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