finding the width of a binary tree
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The width of a binary tree can mean two slightly different things depending on the problem statement. In basic data structure discussions, it often means the maximum number of actual nodes on any level. In interview problems such as LeetCode 662, width usually includes the gaps between the leftmost and rightmost non-null positions on a level.
That second definition is the more interesting one algorithmically. It requires tracking where nodes would appear in a complete binary tree, not just counting how many nodes exist on each level.
Simple Level Width Versus Indexed Width
If you only want the number of nodes on the busiest level, a plain breadth-first traversal is enough. For each level, count how many nodes are in the queue and track the maximum.
That solution is correct for the simple definition, but it is not correct for the indexed definition that includes gaps.
BFS With Position Indexes
For the indexed definition, assign each node the position it would have in a complete binary tree. A common convention is:
- root gets index
0 - left child gets
2 * i - right child gets
2 * i + 1
Then the width of a level is rightmost_index - leftmost_index + 1.
Normalizing indexes at each level keeps the numbers small while preserving the width calculation.
Why Normalization Helps
In a very deep tree, raw index values can grow quickly because they double at each step. Python integers can handle that growth, but normalization still makes the code cleaner and easier to reason about.
By subtracting the leftmost index at the start of each level, you make the first node at that level position 0. Every other node keeps the correct relative distance from it, which is all the width formula actually needs.
Example Tree
Consider this tree:
At the bottom level, the actual node count is 2, but the indexed width is 4 because the nodes occupy positions with gaps between them. That is exactly why a plain node count is insufficient for some problem statements.
Complexity
The indexed BFS solution runs in O(n) time because each node is visited once. Space usage is O(w), where w is the maximum number of nodes held in the queue at any level.
That makes breadth-first search the standard solution for this problem. A depth-first version is possible, but BFS maps more naturally to level widths.
Common Pitfalls
- Counting nodes per level when the problem definition includes null-position gaps.
- Forgetting the
+ 1inrightmost - leftmost + 1. - Letting position indexes grow unnecessarily large instead of normalizing each level.
- Mixing one-based and zero-based index formulas.
- Not handling the empty tree case.
Summary
- Clarify whether width means node count or indexed width with gaps.
- For simple width, count nodes at each level with BFS.
- For indexed width, track complete-tree positions during level-order traversal.
- Compute each level's width as
last_index - first_index + 1. - Normalizing indexes per level keeps the implementation stable on deep trees.
Related reading
- Finding three elements in an array whose sum is closest to a given number
- Finding two non-subsequent elements in array which sum is minimal
- Finding unique numbers from sorted array in less than On
- Finding unreachable sections of a 2D map
- Finding translation and scale on two sets of points to get least square error in their distance?
- First occurrence in a binary search
- Finding whether a point lies inside a rectangle or not
- First appearance in Stern's Diatomic Sequence

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.