797. All Paths From Source to Target

PHOTO EMBED

Sun Mar 19 2023 07:17:52 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

class Solution {
public:
    vector<vector<int>> ans;
    
    void dfs(int curr, int dest, vector<vector<int>>& graph, vector<int> &path)
    {
        path.push_back(curr);
        if(curr==dest)
        {
            ans.push_back(path);
        }
        else
        {
            for(auto x:graph[curr])
            {
                dfs(x, dest, graph, path);
            }
        }
        path.pop_back();
    }
    
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        int n=graph.size()-1;
        
        vector<int> path;
        dfs(0, n, graph, path);
        return ans;
    }
};
content_copyCOPY

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