---
# System prepended metadata

title: Path Sum III
tags: [data_structure_python]

---

---
tags: data_structure_python
---

# Path Sum III

- `pathSum` is in charge of returning the number of paths where the sum of the values along the path equals targetSum.
- `count`is in charge of computing paths where the sum of the values along the path equals targetSum for a given node.

```python=
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
        if root == None:
            return 0
        return self.count(root, targetSum) + self.pathSum(root.left, targetSum) + self.pathSum(root.right, targetSum) 
    
    def count(self, root, target):
        
        if root == None:
            return 0

        return (root.val == target) + self.count(root.left, target - root.val) + self.count(root.right, target - root.val)
```