Skip to content

Commit

Permalink
Update dfs-recursive.js
Browse files Browse the repository at this point in the history
Fix wrong DFS implementation in Go
  • Loading branch information
kounkou authored Nov 25, 2024
1 parent 0a24e48 commit d72fd25
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions logic/graph/dfs-recursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,17 @@ var question = [
dfs(nei);
}
}`,
answerGo: `func dfs(start int) {
visited:= make(map[int]bool)
stk:= []int{ start }
for len(stk) > 0 {
node:= stk[len(stk) - 1]
stk = stk[: len(stk)- 1]
if visited[node] {
continue
}
fmt.Println(node)
answerGo: `func dfs(node int) {
visited[node] = true
fmt.Println(node)
for x := len(graph[node]) - 1; x >= 0; x-- {
nextNode:= graph[node][x]
if !visited[nextNode] {
stk = append(stk, nextNode)
}
for _, nei := range graph[node] {
if visited[nei] {
continue
}
dfs(nei)
}
}
}
`},
]
]

0 comments on commit d72fd25

Please sign in to comment.