# 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]]:
# if not root:
# return []
# path = []
# return self.traversal(root, targetSum - root.val, path)
# def traversal(self, root, count, path):
# if not root.left and not root.right and count == 0:
# return 1
# if not root.left and not root.right:
# return 0
# if root.left:
# count -= root.left.val
# if self.traversal(root.left, count, )
result = []
self.traversal(root, targetSum, [], result)
return result
def traversal(self, node, count, path, result):
if not node:
return
path.append(node.val)
count -= node.val
if not node.left and not node.right and count == 0:
result.append(list(path))
self.traversal(node.left, count, path, result)
self.traversal(node.right, count, path, result)
path.pop()