Binary Tree Level Order Traversal
Difficulty: Medium
You're given the root of a binary tree. Return its node values grouped by level, from top to bottom. Within each level, list values left to right.
Examples
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
Level 0 has just the root (3). Level 1 has its two children (9, 20). Level 2 has 20's two children (15, 7).
Input: root = [1]
Output: [[1]]
A single node is its own entire level.
Input: root = []
Output: []
There are no nodes, so there are no levels at all.
Constraints
The number of nodes in the tree is in the range [0, 2000].
-1000 <= Node.val <= 1000
Approach
Breadth-first search (BFS) visits nodes level by level. You just need to know where each level ends.
Before processing a level, check the queue's size. That's exactly how many nodes are in this level, since none of the next level's nodes have been added yet. Process that many nodes, collect their values, queue their children, and move on.
Solutions
Iterative BFS
Start a queue with the root. Each round, note the queue size: that's this level's node count. Pop that many, record their values, and enqueue their children.
class TreeNode {
constructor(val, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function levelOrder(root) {
if (root === null) return [];
const result = [];
const queue = [root];
while (queue.length > 0) {
const levelSize = queue.length;
const level = [];
for (let i = 0; i < levelSize; i++) {
const node = queue.shift();
level.push(node.val);
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
result.push(level);
}
return result;
}Time: O(n) · Space: O(n) — the queue and the result both hold every node's value
Recursive DFS with Level Tracking
Do a depth-first traversal that passes the current depth along. Use the depth as an index into the result, adding a new sub-array the first time a depth is reached.
function levelOrder(root) {
const result = [];
function dfs(node, depth) {
if (node === null) return;
if (result.length === depth) {
result.push([]);
}
result[depth].push(node.val);
dfs(node.left, depth + 1);
dfs(node.right, depth + 1);
}
dfs(root, 0);
return result;
}Time: O(n) · Space: O(h) — recursion stack depth, plus O(n) for the result array itself