6-line simple python recursive solution that beats 98.6%.


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

  • 0
    I

    You must submit before dawn. During rush hours, this solution only beats 50% or less


Log in to reply
 

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