797. All Paths From Source to Target Medium

@problem@discussion
#Backtracking#Depth-First Search#Breadth-First Search#Graph



1/**
2 * [797] All Paths From Source to Target
3 *
4 * Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order.
5 * The graph is given as follows: graph[i] is a list of all nodes you can visit from node i (i.e., there is a directed edge from node i to node graph[i][j]).
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/28/all_1.jpg" style="width: 242px; height: 242px;" />
9 * Input: graph = [[1,2],[3],[3],[]]
10 * Output: [[0,1,3],[0,2,3]]
11 * Explanation: There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
12 * 
13 * Example 2:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/28/all_2.jpg" style="width: 423px; height: 301px;" />
15 * Input: graph = [[4,3,1],[3,2,4],[3],[4],[]]
16 * Output: [[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]
17 * 
18 *  
19 * Constraints:
20 * 
21 * 	n == graph.length
22 * 	2 <= n <= 15
23 * 	0 <= graph[i][j] < n
24 * 	graph[i][j] != i (i.e., there will be no self-loops).
25 * 	All the elements of graph[i] are unique.
26 * 	The input graph is guaranteed to be a DAG.
27 * 
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/all-paths-from-source-to-target/
32// discuss: https://leetcode.com/problems/all-paths-from-source-to-target/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36impl Solution {
37    pub fn all_paths_source_target(graph: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
38        vec![]
39    }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_797() {
50    }
51}
52


Back
© 2025 bowen.ge All Rights Reserved.