#662
Maximum Width of Binary Tree
specialist · 950 · lc medium +32 · verified · 45.3% accepted · 9,680 likes · top 28%
Description
Given the root of a binary tree, return its maximum width across all levels. A level's width is the span from the leftmost to the rightmost non-null node, counting any null nodes in between as if the tree were a complete binary tree. The answer fits within a 32-bit signed integer.
Example 1:
Input: root = [1,3,2,5,3,null,9]
Output: 4
Explanation: The maximum width exists in the third level with length 4 (5,3,null,9).
Example 2:
Input: root = [1,3,2,5,null,null,9,6,null,7]
Output: 7
Explanation: The maximum width exists in the fourth level with length 7 (6,null,null,null,null,null,7).
Example 3:
Input: root = [1,3,2,5]
Output: 2
Explanation: The maximum width exists in the second level with length 2 (3,2).
Code
1
2
3
4
5
6
7
8
9