0% completed
Problem Statement
Given the root
of a binary tree, find the maximum width
of the tree.
The maximum width
is the widest level
in the tree.
The width of a level is the number of nodes between 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.
You can assume that the result will fit within a 32-bit
signed integer.
Examples
Example 1
- Input: root = [1, 2, 3, 4, null, null, 5]
- Output:
4
- Justification: The maximum width is at the last level between nodes 4 and 5. It counts four positions:
[4, null, null, 5]
.
Example 2
- Input: root = [1, 2, 3, 4, null, 5, 6, null, 7]
- Output:
4
- Justification: The maximum width is between nodes 4 and 6 at level 3, counting four positions:
[4, null, 5, 6]
.
Example 3
- Input: root = [1, 2, null, 3, 4, null, null, 5]
- Output:
2
- Justification: The maximum width is at the third level, between nodes 3 and 4. It counts two positions:
[3, 4]
.
Constraints:
- The number of nodes in the tree is in the range [1, 3000].
- -100 <= Node.val <= 100
Solution
To solve this problem, we will use a level order traversal approach, where we traverse the tree level by level from left to right. During traversal, we keep track of each node's position in a hypothetical "complete binary tree" to account for all gaps (nulls) between nodes at each level. We use a queue to store nodes along with their positions, which allows us to calculate the width of each level easily. At each level, we compare the positions of the leftmost and rightmost nodes to determine the width. The maximum width found across all levels is our answer.
This approach is effective because it leverages a breadth-first search (BFS) technique to explore each level fully before moving to the next, ensuring that we capture the correct width of every level. It is efficient in both time and space, given that it only requires storing nodes at one level at a time, making it well-suited for handling binary trees of reasonable size.
Step-by-Step Algorithm
-
Check if the tree is empty:
- If the root is
null
, return0
as the width.
- If the root is
-
Initialize a queue for level order traversal:
- The queue will store pairs of nodes and their corresponding positions.
-
Add the root node to the queue with position
0
. -
Set a variable
maxWidth
to0
to keep track of the maximum width found. -
While the queue is not empty:
-
Get the number of nodes at the current level (
size
). -
Get the minimum position index at this level (
minIndex
) to normalize positions. -
Initialize two variables
first
andlast
to store the positions of the first and last nodes at this level. -
For each node at the current level:
- Dequeue the node and its position from the queue.
- Normalize the position by subtracting
minIndex
from thecurrentIndex
to avoid overflow issues. - Update
first
with theindex
value if it is the first node at this level. - Update
last
with theindex
value if it is the last node at this level. - Check if the left child exists:
- Add the left child to the queue with position
2 * current_position
.
- Add the left child to the queue with position
- Check if the right child exists:
- Add the right child to the queue with position
2 * current_position + 1
.
- Add the right child to the queue with position
-
Calculate the width of the current level as
last - first + 1
. -
Update
maxWidth
to the maximum of its current value and the width of the current level.
-
-
Return
maxWidth
after processing all levels.
Algorithm Walkthrough
Input: [1, 2, 3, 4, null, null, 5]
Given the binary tree:
1
/ \
2 3
/ \
4 5
Initial Setup:
- Queue:
[(1, 0)]
(Node1
at position0
) - maxWidth:
0
Level 1
- Size:
1
(Only the root node1
) - minIndex:
0
(Position of node1
) - Initialize
first
andlast
to0
.
Process Node 1:
- Dequeue Node
1
with position0
. - Update
first
to0
(first node at this level). - Update
last
to0
(last node at this level). - Enqueue left child (
2
) with position0
. - Enqueue right child (
3
) with position1
. - Calculate Width:
last - first + 1 = 0 - 0 + 1 = 1
- Update maxWidth:
max(0, 1) = 1
.
Level 2
- Size:
2
(Nodes2
and3
) - minIndex:
0
(Position of node2
) - Initialize
first
andlast
to0
.
Process Node 2:
- Dequeue Node
2
with position0
. - Update
first
to0
(first node at this level). - Enqueue left child (
4
) with position0
(no right child).
Process Node 3:
- Dequeue Node
3
with position1
. - Update
last
to1
(last node at this level). - Enqueue right child (
5
) with position2*index + 1 = 2*1 + 1 = 3
(no left child). - Calculate Width:
last - first + 1 = 1 - 0 + 1 = 2
- Update maxWidth:
max(1, 2) = 2
.
Level 3
- Size:
2
(Nodes4
and5
) - minIndex:
0
(Position of node4
) - Initialize
first
andlast
to0
.
Process Node 4:
- Dequeue Node
4
with position0
. - Update
first
to0
(first node at this level).
Process Node 5:
- Dequeue Node
5
with position3
. - Update
last
to3
(last node at this level). - Calculate Width:
last - first + 1 = 3 - 0 + 1 = 4
- Update maxWidth:
max(2, 4) = 4
.
End
- The queue is empty; all levels are processed.
- Return maxWidth:
4
.
Final Output: The maximum width of the binary tree is 4
.
Code
Complexity Analys
Time Complexity
- The time complexity of this solution is O(N), where
N
is the total number of nodes in the binary tree. - This is because we perform a level-order traversal (BFS) of the tree, visiting each node exactly once to determine the maximum width.
Space Complexity
- The space complexity is O(W), where
W
is the maximum width of the binary tree. - The space complexity is determined by the maximum number of nodes that could be stored in the queue at any given level of the tree. In the worst case, this will be the width of the widest level of the binary tree.
Table of Contents
Problem Statement
Examples
Solution
Step-by-Step Algorithm
Algorithm Walkthrough
Code
Complexity Analys
Time Complexity
Space Complexity