forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAll_Paths_From_Source_to_Target.java
29 lines (28 loc) · 1.02 KB
/
All_Paths_From_Source_to_Target.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
void findPaths(int[][] graph,int fromNode,List<Integer> path,List<List<Integer>> ans){
//If fromNode has reached the last Node of graph , add this node to path and return
if(fromNode==graph.length-1)
{
path.add(fromNode);
ans.add(new ArrayList<Integer>(path));
path.remove(path.indexOf(fromNode));
return;
}
//Traverse for all nodes in list of fromNode
for(int i=0;i<graph[fromNode].length;i++){
if(path.size()==0)
path=new ArrayList<Integer>();
//Add the node and call for its list of reachable nodes
path.add(fromNode);
findPaths(graph,graph[fromNode][i],path,ans);
path.remove(path.size()-1);
}
return;
}
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
List<List<Integer>> ans=new ArrayList<List<Integer>>();
//Start from 0 node
findPaths(graph,0,new ArrayList<Integer>(),ans);
return ans;
}
}