Python 4 line recursive solution

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

Log in to reply

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