All Paths From Source to Target - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Tuesday 25 August 2020

All Paths From Source to Target

All Paths From Source to Target

All Paths From Source to Target


 class Solution {

public:

    vector<vector<int>> ans;

    void path(int i,vector<int> &v,vector<bool> &vis,vector< vector<int> > &adj)

    {

          v.push_back(i);

           if(i==adj.size()-1)

           {

               ans.push_back(v);

           }

        

        vis[i]=true;

        for(int j=0;j<adj[i].size();++j)

        {

        path(adj[i][j],v,vis,adj);

        }

        vis[i]=false;

        v.pop_back();

    }

    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {

       

        

        vector<int> res;

        vector<bool> vis(graph.size(),false);

        path(0,res,vis,graph);

        // queue<vector<int>> q;

        // q.push({0});

        // while(!q.empty())

        // {

        //     auto p=q.front();

        //     q.pop();

        //     int j=p.back();

        //     if(graph.size()-1==j)

        //         ans.push_back(p);

        //     else

        //     for(auto i:graph[j])

        //     {    res=p;

        //         res.push_back(i);

        //         q.push(res);

        //     }

        // }


        return ans;

        

    }

};