Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Binary Tree Level Order Traversal
Bookmark
DFS
Two Queues
Level-Size
Input
Example 1
Complete Tree
Four Levels
Custom
tree
=
[3, 9, 20, null, null, 15, 7]
3
9
20
15
7
L0
L1
L2
result[0]
result[1]
result[2]
depth-first traversal, grouped by depth
3
9
20
15
7
L0
L1
L2
result[0]
result[1]
result[2]
depth-first traversal, grouped by depth
3
9
20
15
7
L0
L1
L2
result[0]
3
result[1]
result[2]
depth 0:
result[
0
].append(
3
)
3
9
20
15
7
L0
L1
L2
result[0]
3
result[1]
9
result[2]
depth 1:
result[
1
].append(
9
)
3
9
20
15
7
L0
L1
L2
result[0]
3
result[1]
9
20
result[2]
depth 1:
result[
1
].append(
20
)
3
9
20
15
7
L0
L1
L2
result[0]
3
result[1]
9
20
result[2]
15
depth 2:
result[
2
].append(
15
)
3
9
20
15
7
L0
L1
L2
result[0]
3
result[1]
9
20
result[2]
15
7
depth 2:
result[
2
].append(
7
)
3
9
20
15
7
L0
L1
L2
result[0]
3
result[1]
9
20
result[2]
15
7
[[3], [9,20], [15,7]]
algo
master
.
io
Step:
Start: collect each level's values with DFS + depth
0 / 6
Input
Example 1
Complete Tree
Four Levels
Custom
tree
=
[3, 9, 20, null, null, 15, 7]
0 / 6
algo
master
.
io
Step:
Start: collect each level's values with DFS + depth