java.lang.Object
g0701_0800.s0797_all_paths_from_source_to_target.Solution

public class Solution extends Object
797 - All Paths From Source to Target.<p>Medium</p> <p>Given a directed acyclic graph ( <strong>DAG</strong> ) of <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, find all possible paths from node <code>0</code> to node <code>n - 1</code> and return them in <strong>any order</strong>.</p> <p>The graph is given as follows: <code>graph[i]</code> is a list of all nodes you can visit from node <code>i</code> (i.e., there is a directed edge from node <code>i</code> to node <code>graph[i][j]</code>).</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/09/28/all_1.jpg" alt="" /></p> <p><strong>Input:</strong> graph = [[1,2],[3],[3],[]]</p> <p><strong>Output:</strong> [[0,1,3],[0,2,3]]</p> <p><strong>Explanation:</strong> There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/09/28/all_2.jpg" alt="" /></p> <p><strong>Input:</strong> graph = [[4,3,1],[3,2,4],[3],[4],[]]</p> <p><strong>Output:</strong> [[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == graph.length</code></li> <li><code>2 <= n <= 15</code></li> <li><code>0 <= graph[i][j] < n</code></li> <li><code>graph[i][j] != i</code> (i.e., there will be no self-loops).</li> <li>All the elements of <code>graph[i]</code> are <strong>unique</strong>.</li> <li>The input graph is <strong>guaranteed</strong> to be a <strong>DAG</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • allPathsSourceTarget

      public List<List<Integer>> allPathsSourceTarget(int[][] graph)