Visualize recursive call stacks, queue progressions, and constant space threaded Morris algorithms.
Every node is dequeued and visited once.
W is the maximum width of the tree. In a complete binary tree, the last level contains N/2 nodes, requiring O(N) space in the queue.
levelOrder(root) {if (root === null) return;
queue.enqueue(root);
while (!queue.isEmpty()) {node = queue.dequeue(); visit(node);
if (node.left) queue.enqueue(node.left);
if (node.right) queue.enqueue(node.right);
}
}