Maximum Width of a Binary Tree Level
A tree-view component needs to size its widest row. The width of a level is the span between its leftmost and rightmost present nodes, counting the null gaps between them as if the tree were complete.
Problem
Given the root of a binary tree, return the maximum width among all levels. The width of a level is the number of positions between the leftmost and rightmost non-null nodes (inclusive), where positions follow complete-binary-tree indexing (null nodes in between count toward the width).
Input
The root of a binary tree (given as a level-order array in examples).
Output
An integer: the maximum level width.
Constraints
- The number of nodes is in [1, 3000].
- -100 <= Node.val <= 100
Examples
Example 1
Input
root = [1,3,2,5,3,null,9]
Output
4
The deepest level spans positions for 5,3,_,9 — width 4.
Example 2
Input
root = [1,3,2,5]
Output
2
The level with 5 alone plus its implied sibling span gives width 2.