Maximum Width of Binary Tree
Problem Statement
You are given the root of a binary tree. Find its maximum width.
The width of one level is the number of slots from its leftmost real node to its rightmost real node. The empty slots between them count too, as if the level were part of a complete binary tree that keeps going down.
Return the largest width over all levels. The answer always fits in a 32-bit signed integer.
The root of a binary tree. The tree is never empty.
The largest width of any level, as an integer.
The third level has 5, 3, an empty slot and 9. From 5 to 9 there are 4 slots.
The level with 6 and 7 reads: 6, five empty slots, then 7. That is 7 slots, so the width is 7.
The second level has 3 and 2 side by side, with no gap. Width 2.
- β’ The number of nodes is between 1 and 3000.
- β’ β100 β€ Node.val β€ 100
- β’ The answer fits in a 32-bit signed integer.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Keep each level as a full row of slots, with placeholders for missing nodes, then measure from the first real node to the last
Give each node a heap-style position (left = 2p, right = 2p + 1); a level's width is its last position β its first position + 1
Walk the tree level by level and keep every slot, real or empty. A missing node becomes a placeholder, and a placeholder gets two placeholder children. A level's width is then the number of slots from its first real node to its last one.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Tree BFS translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.