Maximum Width of Binary Tree - Problem
Maximum Width of Binary Tree

Given the root of a binary tree, your task is to find the maximum width among all levels of the tree. Think of this as measuring the "widest spread" of nodes at any horizontal level.

The width of a level is defined as the distance between the leftmost and rightmost non-null nodes at that level, including any null nodes that would exist in between if we were to complete the binary tree structure.

For example, if at level 2 you have nodes at positions 1 and 7, the width would be 7 - 1 + 1 = 7, even if positions 2, 3, 4, 5, and 6 are null.

Input: Root of a binary tree
Output: Maximum width (integer) across all levels

The answer is guaranteed to fit in a 32-bit signed integer.

Input & Output

example_1.py โ€” Simple Tree
$ Input: [1,3,2,5,3,null,9]
โ€บ Output: 4
๐Ÿ’ก Note: The maximum width is at level 2 with nodes at positions 2,3,6,7. The width is 7-2+1 = 6. Wait, let me recalculate: positions are 2,3 for left subtree and 6,7 for right subtree, so width is 7-2+1 = 6. Actually, looking at the standard example, the output should be 4, which means the width calculation considers the span from leftmost to rightmost node.
example_2.py โ€” Single Node
$ Input: [1]
โ€บ Output: 1
๐Ÿ’ก Note: Single node tree has width 1 at level 0.
example_3.py โ€” Left-Heavy Tree
$ Input: [1,3,2,5,null,null,9,6,null,7]
โ€บ Output: 7
๐Ÿ’ก Note: At the deepest level, we have nodes at positions that span a width of 7. The tree is skewed which creates a large width at lower levels.

Constraints

  • The number of nodes in the tree is in the range [0, 3000]
  • -100 โ‰ค Node.val โ‰ค 100
  • The answer is guaranteed to fit in a 32-bit signed integer

Visualization

Tap to expand
Maximum Width of Binary TreeLevel 0:1pos: 0width: 1Level 1:3pos: 02pos: 1width: 2Level 2:5pos: 03pos: 19pos: 3width: 4 โœ“Position Calculation (Complete Binary Tree):โ€ข Root at position 0 โ€ข Left child: 2 ร— parent_pos โ€ข Right child: 2 ร— parent_pos + 1Width Formula:Width = rightmost_position - leftmost_position + 1
Understanding the Visualization
1
Position Assignment
Assign each node a position as if tree were complete: root=0, left_child=2*parent, right_child=2*parent+1
2
Level Processing
Process each level using BFS, tracking the leftmost and rightmost positions
3
Width Calculation
For each level, width = rightmost_position - leftmost_position + 1
4
Maximum Tracking
Keep track of the maximum width encountered across all levels
Key Takeaway
๐ŸŽฏ Key Insight: By treating the binary tree as if it were a complete binary tree and assigning position indices, we can calculate the width of any level using simple arithmetic: rightmost_pos - leftmost_pos + 1
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28 Apple 24
67.2K Views
Medium-High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen