LeetCode 797. All Paths From Source to Target

Problem Statement


impl Solution {
fn dfs(graph: &Vec<Vec<i32>>, cur: i32, p: Vec<i32>, ans: &mut Vec<Vec<i32>>) {
let mut path = p.clone();
path.push(cur);
if cur as usize == graph.len() - 1 {
ans.push(path);
} else {
for neigh in &graph[cur as usize] {
Self::dfs(graph, *neigh, path.clone(), ans);
}
}
}

pub fn all_paths_source_target(graph: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut ans = vec![];
let mut g = graph.clone();
let path = vec![];
Self::dfs(&mut g, 0, path, &mut ans);
return ans;
}
}