#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param root TreeNode类 
# @param sum int整型 
# @return bool布尔型
#
class Solution:
    def hasPathSum(self , root: TreeNode, sum: int) -> bool:
        # write code here
        # 遍历每个节点就三步:1.添加该节点 2.计算是否满足要求(和为sum;是叶子节点)3.弹出该节点
        res = []
        path = []
        def recur(root, tar):
            if not root:
                return None
            path.append(root.val)
            tar = tar - root.val
            if (tar == 0) and (not root.left) and (not root.right):
                res.append(list(path))
            recur(root.left, tar)
            recur(root.right, tar)
            path.pop()  # 当前节点的左孩子和右孩子都遍历完之后才弹出当前节点的值
        recur(root, sum)
        if res:
            return True
        else:
            return False