package leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * https://leetcode.com/problems/all-paths-from-source-to-target/
 */
public class Problem797 {
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        // TODO
        List<List<Integer>> result = new ArrayList<>();
        boolean[] visited = new boolean[graph.length];
        Queue<Integer> queue = new LinkedList<>();
        queue.add(0);
        while (!queue.isEmpty()) {
            Integer current = queue.remove();
            visited[current] = true;
            for (int neighbor : graph[current]) {
                if (!visited[neighbor]) {
                    visited[neighbor] = true;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Problem797 prob = new Problem797();
        System.out.println(prob.allPathsSourceTarget(new int[][]{
            {1, 2}, {3}, {3}, {}
        })); // [[0,1,3],[0,2,3]]
    }
}

@@ -63,63 +63,10 @@
 l.Li
-nkedList;%0Aimport java.util.List;%0Aimport java.util.Queue
+st
 ;%0A%0A/
@@ -237,24 +237,8 @@
 ) %7B%0A
-        // TODO%0A
     
@@ -301,245 +301,341 @@
     
-boolean%5B%5D visited = new boolean%5Bgraph.length%5D
+allPaths(graph, 0, new ArrayList%3C%3E(), result);%0A        return result
 ;%0A    
+%7D%0A%0A
     
-Queue%3CInteger%3E queue = new LinkedList%3C%3E();%0A        queue.add(0);%0A        while (!queue.isEmpty()) %7B%0A            Integer current = queue.remove();%0A            visited%5Bcurrent%5D = true;
+private static void allPaths(int%5B%5D%5B%5D graph, int source, List%3CInteger%3E accu,%0A                                 List%3CList%3CInteger%3E%3E result) %7B%0A        accu.add(source);%0A        if (graph%5Bsource%5D.length == 0) %7B%0A            result.add(accu);%0A        %7D else %7B
 %0A   
@@ -673,15 +673,14 @@
 aph%5B
-current
+source
 %5D) %7B
@@ -700,144 +700,89 @@
     
-if (!visited%5Bneighbor%5D) %7B%0A                    visited%5Bneighbor%5D = true;%0A                %7D%0A            %7D%0A        %7D%0A        return result;
+allPaths(graph, neighbor, new ArrayList%3C%3E(accu), result);%0A            %7D%0A        %7D
 %0A   

Solve problem 797