dfs + bfs, 由于题目中路径不以根节点开始、叶节点结束,设置全局变量保存路径数量

# 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):
        self.path = 0
        
    def FindPath(self , root: TreeNode, s: int) -> int:
        """
        {1,0,1,1,2,0,-1,0,1,-1,0,-1,0,1,0},2 13
        """
        # write code here
        if not root:
            return 0
        q = [root]
        while q:
            node = q.pop(0)
            self.sub_find_path(node, s)
            if node.left:
                q.append(node.left)
            if node.right:
                q.append(node.right)
        return self.path
    
    def sub_find_path(self, root: TreeNode, s: int) -> int:
        if not root:
            return
        if root and s - root.val == 0:
            self.path += 1
        self.sub_find_path(root.left, s - root.val)
        self.sub_find_path(root.right, s - root.val)