# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
classSolution:defhasPathSum(self,root:TreeNode,sum:int)->bool:ifnotroot:returnFalsesum-=root.valifnotroot.leftandnotroot.right:# if reach a leaf
returnsum==0returnself.hasPathSum(root.left,sum)orself.hasPathSum(root.right,sum)