classSolution{ private List<List<Integer>> allPaths = new ArrayList<>();
public List<List<Integer>> allPathsSourceTarget(int[][] graph) { List<Integer> path = new ArrayList<Integer>(); path.add(0); allPathsSourceTarget(graph, 0, graph.length - 1, path); return allPaths; }
publicvoidallPathsSourceTarget(int[][] graph, int k, int n, List<Integer> path){ List<Integer> currentPath = new ArrayList<>(path); if (k == n) { allPaths.add(currentPath); return; } for (int i : graph[k]){ currentPath.add(i); allPathsSourceTarget(graph, i, n, currentPath); currentPath.remove(currentPath.size() - 1); } } }