Problem
Section titled “Problem”Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).
Example 1:

Input: root = [3,9,20,null,null,15,7]Output: [[3],[9,20],[15,7]]Example 2:
Input: root = [1]Output: [[1]]Example 3:
Input: root = []Output: []Constraints:
- The number of nodes in the tree is in the range
[0, 2000]. -
Node.val
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Breadth-first search with a FIFO queue visits nodes in exactly the required order: a node’s children are appended behind everything already waiting, so all nodes of depth d leave the queue before any node of depth d + 1, and siblings leave left to right.
The only extra work is knowing where one level ends. Here is the key invariant: at the start of each round of the outer loop, the queue contains exactly the nodes of one level and nothing else. So we record size = q.size(), pop exactly that many nodes into a new level list, and push their children; when the round ends, the queue holds exactly the next level.
Algorithm
Section titled “Algorithm”- If
rootis null, return an empty list. Otherwise pushrootintoq. - While
qis not empty:- let
size = q.size()and create an emptylevel; - repeat
sizetimes: popnode, appendnode->valtolevel, and pushnode->leftandnode->rightif they exist; - append
leveltolevels.
- let
- Return
levels.
On Example 1 the queue is [3], then [9, 20], then [15, 7], giving the three levels of the output.
Complexity
Section titled “Complexity”- Time: — every node is pushed and popped exactly once.
- Space: extra — the queue holds at most one level plus part of the next, where is the maximum width of the tree (up to about for a complete tree). The output is not counted.
Edge cases
Section titled “Edge cases”- Empty tree (Example 3): returns
[]immediately rather than[[]]. - Missing children: only non-null children are pushed, so
levelnever contains placeholder values.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> levels; if (!root) return levels; queue<TreeNode*> q; q.push(root); while (!q.empty()) { int size = q.size(); vector<int> level; level.reserve(size); for (int i = 0; i < size; i++) { TreeNode* node = q.front(); q.pop(); level.push_back(node->val); if (node->left) q.push(node->left); if (node->right) q.push(node->right); } levels.push_back(move(level)); } return levels; }};class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> levels = new ArrayList<>(); if (root == null) return levels; ArrayDeque<TreeNode> q = new ArrayDeque<>(); q.add(root); while (!q.isEmpty()) { int size = q.size(); List<Integer> level = new ArrayList<>(size); for (int i = 0; i < size; i++) { TreeNode node = q.poll(); level.add(node.val); if (node.left != null) q.add(node.left); if (node.right != null) q.add(node.right); } levels.add(level); } return levels; }}from collections import deque
class Solution: def levelOrder(self, root: TreeNode | None) -> list[list[int]]: levels = [] if not root: return levels q = deque([root]) while q: level = [] for _ in range(len(q)): node = q.popleft() level.append(node.val) if node.left: q.append(node.left) if node.right: q.append(node.right) levels.append(level) return levelsGive it a try first
Attempt the problem on LeetCode — the code will still be here.