Leetcode 797. All Paths From Source to Target

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

1. Description

Leetcode 797. All Paths From Source to Target_第1张图片
All Paths From Source to Target

2. Solution

class Solution {
public:
    vector> allPathsSourceTarget(vector>& graph) {
        vector> paths;
        vector path;
        tranversePath(graph, paths, path, 0, graph.size() - 1);
        return paths;
    }
    
    
    void tranversePath(const vector>& graph, vector>& paths, vector 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/

你可能感兴趣的:(Leetcode 797. All Paths From Source to Target)