# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param root TreeNode类 # @param sum int整型 # @return int整型 # class Solution: def __init__(self) -> None: self.count = 0 def dfs(self,root,sum): if root is None: return False else: if sum==root.val: self.count +=1 leftf = self.dfs(root.left,sum-root.val) rightf = self.dfs(root.right,sum-root.val) return leftf or rightf def FindPath(self , root: TreeNode, sum: int) -> int: # write code here if root is None: return 0 else: get = self.dfs(root,sum) leftf = self.FindPath(root.left,sum) rightf = self.FindPath(root.right,sum) return self.count