class Solution(object):
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 and root.val - sum == 0:
return True
sum -= root.val
return self.hasPathSum(root.left,sum) or self.hasPathSum(root.right,sum)
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 and root.val - sum == 0:
return True
sum -= root.val
return self.hasPathSum(root.left,sum) or self.hasPathSum(root.right,sum)