---
# System prepended metadata

title: Path Sum II
tags: [data_structure_python]

---

---
tags: data_structure_python
---

# Path Sum II

https://leetcode.com/problems/path-sum-ii/

```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) -> List[List[int]]:
        
        def dfs(node, target, tmp):
            if node == None:
                return []
    
            dfs(node.left, target - node.val, tmp + [node.val])
            dfs(node.right, target - node.val, tmp + [node.val])
            
            if node.left == None and node.right == None and target == node.val:
                res.append(tmp + [node.val])
            
        res = []
        dfs(root, targetSum, [])
        return res
```