my 79ms solution in python3

  • 0
    class Solution:
        def hasPathSum(self, root, sum):
            if not root:
                return False
            if not (root.left or root.right) and root.val == sum:
                return True
            return self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val)

Log in to reply

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