# 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]]:
ret = []
tmp = []
def dfs(x, rest):
if not x:
return
rest -= x.val
tmp.append(x.val)
if not x.left and not x.right:
if rest == 0:
ret.append(tmp[:])
dfs(x.left, rest)
dfs(x.right, rest)
rest += x.val
tmp.pop()
dfs(root, targetSum)
return ret