Leetcode 797. All Paths From Source to Target

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

1. Description

All Paths From Source to Target

2. Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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/
如果有收获,可以请我喝杯咖啡!