# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if not root:
return False
sum = sum - root.val
if not root.left and not root.right and sum==0:
return True
left = self.hasPathSum(root.left,sum)
right = self.hasPathSum(root.right,sum)
return left or right