 All Problems
Maximum Width of Binary Tree
medium
tree
depth-first search
breadth-first search
binary tree
amazon
google
facebook

Given the root of a binary tree, return the maximum width of the given tree. The width of one level is defined as the length between the leftmost and rightmost non-null node, including the null nodes in between. The answers will fit in a 32-bit signed integer.

Example 1:

Input:  1 3 2 5 3 null 9
Output: 4
Explanation: Level 3 has width 4 (positions 5, null, null, 9).

Example 2:

Input:  1 3 null 5 3
Output: 2

Constraints:

  • The number of nodes is in the range [1, 3000]
  • -100 ≤ Node.val ≤ 100

Input format: BFS level-order, space-separated, null for missing nodes.

Output format: Maximum width.

Run to check your code against the sample cases, or submit to run every case