This practice workspace is built for desktop. Open it on a larger screen to start solving.
Maximum Width of Binary Tree
Given the root of a binary tree, return the maximum width of the given tree.
The maximum width of a tree is the maximum width among all levels.
The width of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes that would be present in a complete binary tree extending down to that level are also counted into the length calculation.
Equivalently, assign each node an index as if the tree were a heap (root = 1, left child of i is 2i, right child is 2i + 1). The width of a level is max_index - min_index + 1. Return the max across all levels.
Input convention. Level-order array with null for missing children. The starter includes TreeNode and arrayToTree.
Empty tree returns 0.
Examples
Constraints
- The number of nodes is in the range [0, 3000].
- -100 <= Node.val <= 100
Preview Mode
This is a read-only preview of DSAMind's AI coaching. Sign up to get custom feedback on your own solution.
Your approach
- Pattern:Breadth-First Search
- Time:O(N)
- Space:O(1)
Complete approach — pattern and complexity both named.
Coach
What you got right
You correctly matched the sorted input requirement and implemented a linear-time scan using two pointers.
Where it diverged
No divergence detected. The code correctly aligns with the stated approach.
Next attempt: focus on
Try solving related sliding window problems to build familiarity with two-pointer variants.
Signals
No signals fired — clean run, you stayed in flow.