Given the root of a binary tree, return the level-order traversal of its node values, grouped per level (left-to-right).
Use a queue seeded with the root. At each iteration, snapshot len(queue) — that's the width of the current level — then pop exactly that many nodes, collecting their values into one list while enqueuing their children for the next level.
[]
[0, 10⁴].-1000 ≤ Node.val ≤ 1000Snapshot the queue length at the start of each level — that fixes the per-level boundary.
The answer lists the root, then everything one step below it, then everything two steps below, each row from left to right. So the work is to visit nodes by distance from the root, and to know where one distance ends and the next begins.
Time O(n)Space O(w)
The full walkthrough and code are part of Premium