Maximum Width of Binary Tree
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Given the root of a binary tree, return its **maximum width**. The width of one level is the number of positions between its leftmost and rightmost non-null nodes, inclusive, counting the empty positions in between as if the tree at that depth were a complete binary tree.
Put differently, number the positions at each depth as a full binary tree would (a node at position p has children at 2p and 2p + 1). The width of a level is rightmostPosition - leftmostPosition + 1, and the answer is the largest width over all levels.
The answer is guaranteed to fit in a signed 32-bit integer. Aim for O(n) time with a level-order traversal.
Examples
Constraints
- 1 <= number of nodes <= 3000
- -100 <= Node.val <= 100
- The answer fits in a signed 32-bit integer
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.