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