Bug of the the testing?


  • 0
    W

    [1,2]
    1
    Output:
    true
    Expected:
    false

    I think it should be the true, because the root value is 1.
    When [1], 1, the output is true.

    class Solution():
    def hasPathSum(self,root,target):
        if root==None:
            return False
        else:
            return self.helper(root,target)
    
    def helper(self,root,target):
        if root==None and target ==0:
            return True
        if root ==None and target!=0:
            return False
        return self.helper(root.left,target-root.val) or self.helper(root.right,target-root.val)

  • 2

    You forgot that we're looking for a root-to-leaf path. In [1,2], the 1 is no leaf.


  • -1
    N

    I have this problem too. It's weird because in the example they do count the root as part of the sum
    From the instructions: "return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22."


Log in to reply
 

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