Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Maximum Width of Binary Tree
Bookmark
BFS
DFS
Input
Example 1 (width 4)
Wide Gap (width 7)
Complete Tree (width 4)
Custom
tree
=
[1, 3, 2, 5, 3, null, 9]
1
3
2
5
3
9
L0
L1
L2
max = 0
queue
width counts the gaps between the ends
1
3
2
5
3
9
L0
L1
L2
max = 0
queue
width counts the gaps between the ends
1
3
2
5
3
9
L0
L1
L2
max = 0
queue
1
0
front
level 0:
leftmost index =
0
1
3
2
5
3
9
i=0
L0
L1
L2
max = 0
queue
3
0
2
1
front
enqueue
2
index = 2 * 0 + 1 =
1
1
1
3
2
5
3
9
i=0
L0
L1
L2
max = 1
queue
3
0
2
1
front
max_width =
1
1
1
3
2
5
3
9
i=0
L0
L1
L2
max = 1
queue
2
1
5
0
front
enqueue
5
index = 2 * 0 =
0
1
1
3
2
5
3
9
i=1
L0
L1
L2
max = 1
queue
5
0
3
1
front
node
2
at index
1
1
2
1
3
2
5
3
9
i=1
L0
L1
L2
max = 2
queue
5
0
3
1
9
3
front
max_width =
2
1
2
1
3
2
5
3
9
i=0
L0
L1
L2
max = 2
queue
3
1
9
3
front
node
5
at index
0
1
2
4
1
3
2
5
3
9
i=3
L0
L1
L2
max = 2
queue
width =
3 - 0 + 1
=
4
1
2
4
1
3
2
5
3
9
L0
L1
L2
max = 4
queue
return 4
algo
master
.
io
Step:
Start: find the maximum width of the binary tree
0 / 22
Input
Example 1 (width 4)
Wide Gap (width 7)
Complete Tree (width 4)
Custom
tree
=
[1, 3, 2, 5, 3, null, 9]
0 / 22
algo
master
.
io
Step:
Start: find the maximum width of the binary tree