MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / bfs

Function bfs

javascript/1448-count-good-nodes-in-binary-tree.js:43–59  ·  view source on GitHub ↗
(queue, total = 0)

Source from the content-addressed store, hash-verified

41}
42
43const bfs = (queue, total = 0) => {
44 while (queue.length) {
45 for (let i = (queue.length - 1); 0 <= i; i--) {
46 let [ root, max ] = queue.shift();
47
48 const isGood = max <= root.val;
49 if (isGood) total++;
50
51 max = Math.max(max, root.val);
52
53 if (root.right) queue.push([ root.right, max ]);
54 if (root.left) queue.push([ root.left, max ]);
55 }
56 }
57
58 return total;
59}

Callers 1

goodNodesFunction · 0.70

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected