Problem
Section titled “Problem”Given the root of a binary tree, return the maximum width of the given tree.
The maximum width of a tree is the maximum width among all levels.
The width of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes that would be present in a complete binary tree extending down to that level are also counted into the length calculation.
It is guaranteed that the answer will in the range of a 32-bit signed integer.
Example 1:

Input: root = [1,3,2,5,3,null,9]Output: 4Explanation: The maximum width exists in the third level with length 4 (5,3,null,9).Example 2:

Input: root = [1,3,2,5,null,null,9,6,null,7]Output: 7Explanation: The maximum width exists in the fourth level with length 7 (6,null,null,null,null,null,7).Example 3:

Input: root = [1,3,2,5]Output: 2Explanation: The maximum width exists in the second level with length 2 (3,2).Constraints:
- The number of nodes in the tree is in the range .
-
Node.val
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Number the nodes as in an array-backed heap: if a node has position p, its left child gets 2 * p + 1 and its right child 2 * p + 2. These are exactly the slots the nodes would occupy in a complete binary tree, so the missing (null) nodes between two nodes of a level are counted automatically: the width of a level is last position - first position + 1.
The catch is size. Positions double on every level, and a chain of 3000 nodes would need positions near . But only differences within a level matter, so before numbering the children we subtract the level’s first position, start. After the shift every position on a level is less than that level’s width, which the problem guarantees fits in 32 bits, so the children’s positions 2 * p + 2 always fit in a 64-bit integer.
Algorithm
Section titled “Algorithm”- Level-order traversal with a queue
qof{node, position}, starting with{root, 0}. - At the start of each level, read
start = q.front().secondandend = q.back().second, and updateanswithend - start + 1. - Pop the
sizenodes of the level. For each, letp = position - start, and push the non-null children with positions2 * p + 1and2 * p + 2. - Return
answhen the queue is empty.
Walkthrough
Section titled “Walkthrough”Example 2, with the position each node gets when it is pushed (computed from its parent’s re-based position p):
| Level | start, end | Width | Re-based p used for the children |
|---|---|---|---|
| 1 | 0, 0 | 1 | 1 → 0 |
| 2 | 1, 2 | 2 | 3 → 0, 2 → 1 |
| 3 | 1, 4 | 4 | 5 → 0, 9 → 3 |
| 4 | 1, 7 | 7 | — |
Level 3: 5 = 2·0 + 1 and 9 = 2·1 + 2. Level 4: 6 = 2·0 + 1 and 7 = 2·3 + 1, so the width is 7 - 1 + 1 = 7.
Complexity
Section titled “Complexity”- Time: — every node is pushed and popped once.
- Space: — the queue holds at most one full level, where
wis the largest number of nodes on a level.
Edge cases
Section titled “Edge cases”- Single node: one level with
start == end, so the width is1. - Deep, skewed trees: without re-basing, positions would overflow after about 63 levels; with it they stay below .
- Gaps between subtrees (Example 2, level 4): handled by the position arithmetic, not by pushing null nodes, so the queue never grows beyond the real nodes.
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: int widthOfBinaryTree(TreeNode* root) { queue<pair<TreeNode*, long long>> q; q.push({root, 0}); long long ans = 0; while (!q.empty()) { int size = q.size(); long long start = q.front().second, end = q.back().second; ans = max(ans, end - start + 1); for (int i = 0; i < size; ++i) { auto [node, pos] = q.front(); q.pop(); long long p = pos - start; // re-base so positions stay small if (node->left) q.push({node->left, 2 * p + 1}); if (node->right) q.push({node->right, 2 * p + 2}); } } return ans; }};class Solution { private record Entry(TreeNode node, long pos) {}
public int widthOfBinaryTree(TreeNode root) { ArrayDeque<Entry> q = new ArrayDeque<>(); q.add(new Entry(root, 0)); long ans = 0; while (!q.isEmpty()) { int size = q.size(); long start = q.peekFirst().pos(), end = q.peekLast().pos(); ans = Math.max(ans, end - start + 1); for (int i = 0; i < size; i++) { Entry e = q.poll(); long p = e.pos() - start; // re-base so positions stay small if (e.node().left != null) q.add(new Entry(e.node().left, 2 * p + 1)); if (e.node().right != null) q.add(new Entry(e.node().right, 2 * p + 2)); } } return (int) ans; }}from collections import deque
class Solution: def widthOfBinaryTree(self, root: TreeNode | None) -> int: q = deque([(root, 0)]) ans = 0 while q: start, end = q[0][1], q[-1][1] ans = max(ans, end - start + 1) for _ in range(len(q)): node, pos = q.popleft() p = pos - start # re-base so positions stay small if node.left: q.append((node.left, 2 * p + 1)) if node.right: q.append((node.right, 2 * p + 2)) return ansGive it a try first
Attempt the problem on LeetCode — the code will still be here.
