leetcode797+所有0->N-1的路径,递归DFS

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013554860/article/details/88047179

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

//0 -> N-1
class Solution {
public:
    void helper(vector<vector<int>>& graph, int cur, vector<int>path, vector<vector<int>>&res)
    {
        path.push_back(cur);
        if(cur==graph.size()-1) res.push_back(path);
        else for(int neighbor:graph[cur]) {
            helper(graph, neighbor, path, res); //递归树下的每一个节点到N-1
        }
    }
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        vector<vector<int>> res;
        helper(graph, 0, {}, res);
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/u013554860/article/details/88047179