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

Function dfs

graphs/sum-of-distances-in-tree.js:40–51  ·  view source on GitHub ↗
(cur, parent, depth, adjList, treeInfo, count)

Source from the content-addressed store, hash-verified

38}
39
40function dfs(cur, parent, depth, adjList, treeInfo, count) {
41 let sum = 1;
42
43 for (const child of adjList[cur]) {
44 if (child !== parent) {
45 sum += dfs(child, cur, depth + 1, adjList, treeInfo, count);
46 treeInfo.root += depth + 1;
47 }
48 }
49 count[cur] = sum;
50 return sum;
51}
52
53function createAdjacencyList(n, edges) {
54 const list = {};

Callers 1

sumOfDistancesInTreeFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected