Advertisement
vaibhav1906

All paths from source to target

Aug 24th, 2022
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. class Solution {
  2. public:
  3.    
  4.     void dfs(vector<vector<int>>& graph,vector<vector<int>> &ans,vector<int> curr, int node, int n){
  5.        
  6.         curr.push_back(node);
  7.        
  8.         if(node==n-1){
  9.             ans.push_back(curr);
  10.             return;
  11.         }
  12.        
  13.         for(int i = 0; i<graph[node].size(); i++){
  14.            
  15.             int newNode= graph[node][i];
  16.            
  17.             dfs(graph, ans, curr, newNode, n);
  18.            
  19.            
  20.         }
  21.        
  22.         return;
  23.        
  24.     }
  25.    
  26.     vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
  27.        
  28.         vector<vector<int>> ans;
  29.         vector<int> curr;
  30.        
  31.        
  32.         dfs(graph, ans, curr, 0, graph.size());
  33.        
  34.        
  35.         return ans;
  36.        
  37.     }
  38. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement