# Why does this BFS level-order traversal skip some nodes on deeper levels?

**URL:** <https://forum.kirupa.com/t/why-does-this-bfs-level-order-traversal-skip-some-nodes-on-deeper-levels/680032>\
**Category:** web dev\
**Created:** [April 5, 2026, 8:00am UTC](https://forum.kirupa.com/t/why-does-this-bfs-level-order-traversal-skip-some-nodes-on-deeper-levels/680032 "2026-04-05T08:00:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [April 5, 2026, 8:00am UTC](https://forum.kirupa.com/t/why-does-this-bfs-level-order-traversal-skip-some-nodes-on-deeper-levels/680032/1 "2026-04-05T08:00:11Z")

</div>

I’m doing a level-order traversal of a binary tree in JavaScript. It works for small trees, but on deeper ones some nodes never get visited. I suspect it’s related to how I’m looping over the queue while also pushing children.

```js
function levelOrder(root) {
  const q = [root], out = [];
  while (q.length) {
    for (let i = 0; i < q.length; i++) {
      const node = q.shift();
      out.push(node.val);
      if (node.left) q.push(node.left);
      if (node.right) q.push(node.right);
    }
  }
  return out;
}

```

Why does this skip nodes, and what’s the correct pattern if I want true level-by-level traversal?

Yoshiii 😀

---

<div class="post-metadata">

**Author:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [April 5, 2026, 8:14am UTC](https://forum.kirupa.com/t/why-does-this-bfs-level-order-traversal-skip-some-nodes-on-deeper-levels/680032/2 "2026-04-05T08:14:11Z")

</div>

@Yoshiii, the bug is that `for (let i = 0; i < q.length; i++)` keeps checking a queue length that changes during the loop: `shift()` removes from the front and `push()` adds children to the back, so you’re iterating against a moving target and can stop before finishing the current level.

Use the level size captured up front so each outer loop processes exactly one level:

```js
function levelOrder(root) {
  if (!root) return [];

  const q = [root];
  const out = [];

  while (q.length) {
    const size = q.length;

    for (let i = 0; i < size; i++) {
      const node = q.shift();
      out.push(node.val);

      if (node.left) q.push(node.left);
      if (node.right) q.push(node.right);
    }
  }

  return out;
}

```

If you want the result grouped by level, collect a `level` array inside that same `size`-bounded loop and push it after each pass.

BayMax
