Leetcode 797. All Paths From Source to Target

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

《Leetcode 797. All Paths From Source to Target》 All Paths From Source to Target

2. Solution

class Solution {
public:
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        vector<vector<int>> paths;
        vector<int> path;
        tranversePath(graph, paths, path, 0, graph.size() - 1);
        return paths;
    }
    
    
    void tranversePath(const vector<vector<int>>& graph, vector<vector<int>>& paths, vector<int> path, int current, int target) {
        path.push_back(current);
        int length = graph.size();
        if(current == target) {
            paths.push_back(path);
            return;
        }
        for(int i = 0; i < graph[current].size(); i++) {
            tranversePath(graph, paths, path, graph[current][i], target);
        }
    }

};

Reference

  1. https://leetcode.com/problems/all-paths-from-source-to-target/description/
    原文作者:SnailTyan
    原文地址: https://www.jianshu.com/p/8a3066ae2865
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞