862. Find Maximum Width of Binary Tree

EasyTreesBinary TreeBFS

Given the root of a binary tree, return its maximum width — the maximum number of nodes spanning any single level, counting the gaps (null positions) between the leftmost and rightmost non-null nodes of that level. Positions are assigned as in a complete tree. The tree is given as a level-order array.

Input: A level-order array of the tree.

Output: Integer — the maximum width.

Examples

Example 1
Input: [1,3,2,5,3,null,9]
Output: 4
Explanation: The bottom level spans 4 positions.
Example 2
Input: [1]
Output: 1
Explanation: Single node.
Example 3
Input: []
Output: 0
Explanation: Empty tree.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →