```
# 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 __init__(self):
self.count = 0
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: int
"""
self.helper(root, sum)
return self.count
def helper(self, root, sum):
if not root: return None
result = [ root.val ]
left, right = self.helper(root.left, sum), self.helper(root.right, sum)
if left: result += [ x + root.val for x in left ]
if right: result += [ x + root.val for x in right ]
if result.count(sum): self.count += result.count(sum)
return result
```