MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / traverseNode

Function traverseNode

graphs/riverSizes.js:61–79  ·  view source on GitHub ↗
(row, col, matrix, visited, sizes)

Source from the content-addressed store, hash-verified

59}
60
61function traverseNode(row, col, matrix, visited, sizes) {
62 let currentRiverSize = 0;
63 const nodesToExplore = [[row, col]]; //stack
64
65 while (nodesToExplore.length) {
66 let currentNode = nodesToExplore.pop();
67 row = currentNode[0];
68 col = currentNode[1];
69 if (visited[row][col]) continue;
70 visited[row][col] = true;
71 if (matrix[row][col] === 0) continue;
72 currentRiverSize++;
73 const unvisitedNeighbors = getUnvisitedNeighbors(row, col, matrix, visited);
74 for (let neighbor of unvisitedNeighbors) {
75 nodesToExplore.push(neighbor);
76 }
77 }
78 if (currentRiverSize > 0) sizes.push(currentRiverSize);
79}
80
81function getUnvisitedNeighbors(row, col, matrix, visited) {
82 const unvisitedNeighbors = [];

Callers 1

riverSizesFunction · 0.85

Calls 3

getUnvisitedNeighborsFunction · 0.85
popMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected