← DiffPush

Maximum Width of Binary Tree

DiffPush Tier Binary Trees · Hard O(n) · O(n)

The Loading Dock Span Audit

A fulfilment hub draws its storage map as a binary tree, and safety inspectors measure each floor's span by counting the gaps between its outermost occupied bays — phantom bays in between still count toward the span. The audit report must publish the widest floor ever measured across the whole warehouse.

Input: A binary tree as a level-order array (null marks a missing child).

Output: Return the maximum width — the largest count of node slots, including null gaps, between the leftmost and rightmost node of any level.

Constraints

Examples

Example 1

Input: {"tree":[1,3,2,5,3,null,9]}
Output: 4
The last level stretches from slot 0 (5) to slot 3 (9): 5, phantom, phantom, 9 — a span of 4.

Example 2

Input: {"tree":[1,3,null,5,3]}
Output: 2
The deepest level holds only 5 and 3 side by side, so the widest span is 2.

Solve this in your browser →

Also on LeetCode ↗