# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution(object):
    def pathSum(self, root, sum):
        def find_path(root,sum):
            if not root:
                return 0
            if root.val==sum:
                index=1
            else:
                index=0
            return index+find_path(root.left,sum-root.val)+find_path(root.right,sum-root.val)
        if root:
            return find_path(root,sum)+self.pathSum(root.left,sum)+self.pathSum(root.right,sum)
        return 0