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.
The answer is guaranteed to fit in a 32-bit signed integer.
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 treeOutput: Maximum width (integer) across all levelsThe 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
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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code