Advertisement
sak1b

https://leetcode.com/problems/all-paths-from-source-to-target/submissions/

Dec 2nd, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. class Solution {
  2. public:
  3. //int visited[15];
  4.  
  5. vector<vector<int>> res;
  6.  
  7. int final_node;
  8.  
  9. void dfs(int v, vector<int> temp, vector<vector<int>>& graph,int visited[15]) {
  10.  
  11. // cout<<"---dfs: "<<v<<endl;
  12.  
  13. visited[v] = 1;
  14. temp.push_back(v);
  15.  
  16. if(v==final_node){
  17. res.push_back(temp);
  18. }
  19.  
  20. int sz=graph[v].size();
  21.  
  22. for(int i=0;i<sz;i++) {
  23. if (visited[graph[v][i]] == 0){
  24. // cout<<v<<" is calling "<<graph[v][i]<<" calling "<<endl;;
  25. dfs(graph[v][i],temp,graph,visited);
  26.  
  27. }
  28.  
  29. }
  30. visited[v]=0;
  31. }
  32.  
  33. vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
  34.  
  35. final_node = graph.size()-1;
  36. int visited[15];
  37.  
  38. for(int i=0;i<15;i++){
  39. visited[i]=0;
  40. }
  41.  
  42. vector<int> l;
  43. // l.push_back(0);
  44. dfs(0,l,graph,visited);
  45.  
  46.  
  47.  
  48. return res;
  49. }
  50. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement