1def path_sum(root, targetSum):
2 result = []
3
4 def dfs(node, curr_sum, target_sum, path):
5 if not node:
6 return
7 curr_sum += node.val
8 path.append(node.val)
9 if not node.left and not node.right:
10 if curr_sum == target_sum:
11 result.append(list(path))
12 dfs(node.left, curr_sum, target_sum, path)
13 dfs(node.right, curr_sum, target_sum, path)
14 path.pop()
15
16 dfs(root, 0, targetSum, [])
17 return result