All Paths From Source to Target

Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order. The graph is given such that graph[i] is a list of all nodes you can visit from node i.

Constraints

  • n == graph.length
  • 2 <= n <= 15
  • 0 <= graph[i][j] < n
  • graph[i][j] != i
  • All elements of graph[i] are unique
  • The input graph is guaranteed to be a DAG
Saving… · 3 lines
class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:

Console

Click Run to execute sample tests.