All Paths from Source to Target
ID: 797
Last updated
ID: 797
Last updated
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 as follows: graph[i]
is a list of all nodes you can visit from node i
(i.e., there is a directed edge from node i
to node graph[i][j]
).
Start from the start node, do dfs on this node and if there's a valid path exists, add this path to result.
When a valid path found, add to result as a new ArrayList to prevent reference update in future operations
When done with dfs from a given node, remove the last element from the current path list to proceed on to other nodes