Skip to content

Commit 6b7ff2e

Browse files
committed
[Code Commit]
1 parent a7828ea commit 6b7ff2e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Question - All Paths From Source to Target
3+
Link - > https://leetcode.com/explore/featured/card/july-leetcoding-challenge/547/week-4-july-22nd-july-28th/3400/
4+
*/
5+
6+
class Solution {
7+
func allPathsSourceTarget(_ graph: [[Int]]) -> [[Int]] {
8+
var result = [[Int]]()
9+
dfs(graph, 0, [0], &result)
10+
return result
11+
}
12+
13+
func dfs(_ graph: [[Int]],_ index: Int,_ path: [Int],_ result: inout [[Int]]) {
14+
if index == graph.count - 1{
15+
result.append(path)
16+
return
17+
}
18+
19+
for node in graph[index]{
20+
var path = path
21+
path.append(node)
22+
dfs(graph, node, path, &result)
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)