Update depth-first-search.english.md (#34856)
Fixed formatting for the solution to increase readability. It was previously difficult to see since the code was all one line
This commit is contained in:
parent
bc50e4e6da
commit
ce63de866c
@ -76,7 +76,26 @@ console.log(dfs(exDFSGraph, 3));
|
||||
|
||||
|
||||
```js
|
||||
function dfs(graph, root) { var stack = []; var tempV; var visited = []; var tempVNeighbors = []; stack.push(root); while (stack.length > 0) { tempV = stack.pop(); if (visited.indexOf(tempV) == -1) { visited.push(tempV); tempVNeighbors = graph[tempV]; for (var i = 0; i < tempVNeighbors.length; i++) { if (tempVNeighbors[i] == 1) { stack.push(i); }}}} return visited;}
|
||||
function dfs(graph, root) {
|
||||
var stack = [];
|
||||
var tempV;
|
||||
var visited = [];
|
||||
var tempVNeighbors = [];
|
||||
stack.push(root);
|
||||
while (stack.length > 0) {
|
||||
tempV = stack.pop();
|
||||
if (visited.indexOf(tempV) == -1) {
|
||||
visited.push(tempV);
|
||||
tempVNeighbors = graph[tempV];
|
||||
for (var i = 0; i < tempVNeighbors.length; i++) {
|
||||
if (tempVNeighbors[i] == 1) {
|
||||
stack.push(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return visited;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
Loading…
x
Reference in New Issue
Block a user