Maximum Width of Binary Tree
Given the root of a binary tree, return the maximum width among all levels. The width of a level is the distance between its leftmost and rightmost non-null nodes, counting the null positions between them as if the tree were a complete binary tree.
Open official problem prompt ↗Find the widest level of the tree, treating missing nodes between real ones as occupied slots.
Numbering seats in a theater row as if the row were full; the width of a row is the seat number of the rightmost person minus the leftmost person plus one, even if seats between them are empty.
- Input
- root = [1,3,2,5,3,null,9]
- Output
- 4
- Why
- On the bottom level, nodes 5, 3, and 9 occupy positions 0, 1, and 3 of a complete tree, giving width 3 - 0 + 1 = 4.
The number of nodes is in the range [1, 3000]-100 <= Node.val <= 100