Browse curriculum

Maximum Width of Binary Tree

Find the widest level of the tree (including nulls between end nodes).

Medium
15 min
Solve on LeetCode

Examples

Input:root = [1,3,2,5,3,null,9]
Output:4
Explain:

The last level spans slots 0 to 3, including one empty slot.

Input:root = [1,3,2,5,null,null,9,6,null,7]
Output:7
Explain:

The last level spans slots 0 to 6: 6 and 7 with five empty slots between.

Input:root = [1,3,2,5]
Output:2
Explain:

The second level, 3 and 2, is widest.

Problem Understanding

Find the maximum width of the binary tree under root, taken over all of its levels.

  • Width: The number of nodes between the leftmost and rightmost non-null nodes in a level, including the null nodes between them.
  • Imagine filling the tree into a complete binary tree structure. We count the distance from Start to End index.

Algorithm Strategy

Give every node the index it would have in a complete binary tree, and the gaps take care of themselves.

  1. Indices: the root is 0. A node at index i has its left child at 2i and its right child at 2i + 1. Two nodes on a level with indices 0 and 3 have two empty slots between them.
  2. BFS with pairs: the queue holds (node, index).
  3. Width at the start of a level: the front of the queue holds the smallest index, first, and the back holds the largest, last. The width is last - first + 1.
  4. Keep indices small: as each node is dequeued, subtract first from its index before computing its children's. Every level then starts near 0, however deep the tree is.

Interactive Visualization

Every node shows the index it was enqueued with. When a level starts, its slots are laid out beside the tree from first to last — nodes and ∅ gaps alike — and the width is the number of slots. Try Example 2, where the last level has five empty slots between its two nodes.

Dry Run

Tree: [1, 3, 2, 5, 3, null, 9] (LeetCode example 1).

Level Queue at start (value, index) first last width best
0 (1, 0) 0 0 1 1
1 (3, 0), (2, 1) 0 1 2 2
2 (5, 0), (3, 1), (9, 3) 0 3 4 4

Slot 2 on the last level is empty — it is where 2's left child would be — but it lies between the first and last nodes, so it counts. The method returns 4.

Edge Cases & Common Mistakes

Edge Cases:

  • Single node: width 1.
  • Index size: subtracting first keeps indices below twice the widest level seen so far, and LeetCode guarantees the answer fits in a 32-bit integer — but twice that does not, so Java, C++ and C keep indices in 64-bit integers. Python integers and JavaScript numbers hold them exactly.

Common Mistakes:

  • Counting only the non-null nodes on a level. The empty slots between the first and last node are part of the width.
  • Measuring the width after the level has been dequeued, when the queue already holds the next level.

Solution Code

BFS with Indexing Normalization.

Maximum Width of Binary Tree Code
1var widthOfBinaryTree = function(root) {
2 let best = 0;
3 const queue = [[root, 0]];
4 let head = 0;
5 while (head < queue.length) {
6 const size = queue.length - head;
7 const first = queue[head][1];
8 const last = queue[queue.length - 1][1];
9 best = Math.max(best, last - first + 1);
10 for (let i = 0; i < size; i++) {
11 let [node, index] = queue[head++];
12 index -= first;
13 if (node.left) {
14 queue.push([node.left, 2 * index]);
15 }
16 if (node.right) {
17 queue.push([node.right, 2 * index + 1]);
18 }
19 }
20 }
21 return best;
22};

The approach, step by step

  1. Index the root

    Enqueue the root with index 0.

  2. Measure each level

    Before dequeuing a level, take first from the front and last from the back; the width is last − first + 1.

  3. Index the children

    Dequeue each node, subtract first from its index i, and enqueue its children at 2i and 2i + 1.

Frequently asked questions

Why do the null gaps count toward the width?

The problem defines width as the distance between the leftmost and rightmost nodes of a level as if the tree were complete, so empty slots between them count as positions.


Why subtract first from each index?

Indices double with every level, so a deep tree would produce huge numbers. Subtracting the level’s first index as each node is dequeued keeps every level’s indices close to 0 without changing any width.


Why use 64-bit indices in Java, C++ and C?

The answer fits in 32 bits, but a child’s index can be about twice the width of its parent’s level. 64-bit integers hold that intermediate value safely.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description