```
class Solution(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: int
"""
self.sum = sum
self.result = 0
if not root:
return 0
self.dfs(root, [])
return self.result
def dfs(self, node, vl):
if not node:
return
vl = [i+node.val for i in vl] + [node.val]
self.result += vl.count(self.sum)
self.dfs(node.left, vl)
self.dfs(node.right, vl)
return
```