2
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {3
List<List<Integer>> ans = new ArrayList<>();4
pathSum(root, targetSum, new ArrayList<>(), ans);8
public void pathSum(TreeNode root, int targetSum, List<Integer> path, List<List<Integer>> ans) {9
if (root == null) return;13
&& targetSum == root.val) // leaf node that completes path17
path)); // we use new ArrayList because if we don't the originaly List is added which18
// is mutable, if we add a copy that's not mutable.20
pathSum(root.left, targetSum - root.val, path, ans);21
pathSum(root.right, targetSum - root.val, path, ans);23
path.remove(path.size() - 1); // removal of redundant nodes