Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Count Complete Tree Nodes
Bookmark
Linear DFS
Height Compare
Binary Search
Input
6 Nodes
10 Nodes
Perfect Tree (15)
Single Node
Custom
tree
=
[1, 2, 3, 4, 5, 6]
1
2
3
4
5
6
count = 0
visit
every node
: 1 + count(left) + count(right)
1
2
3
4
5
6
count = 0
visit
every node
: 1 + count(left) + count(right)
1
2
3
4
5
6
count = 0
visit
1
1
2
3
4
5
6
count = 0
visit
4
1
2
3
4
5
6
count = 1
subtree size =
1
, total = 1
1
2
3
4
5
6
count = 2
subtree size =
1
, total = 2
1
2
3
4
5
6
count = 3
subtree size =
3
, total = 3
1
2
3
4
5
6
count = 3
visit
6
1
2
3
4
5
6
count = 4
subtree size =
1
, total = 4
1
2
3
4
5
6
count = 6
subtree size =
6
, total = 6
1
2
3
4
5
6
count = 6
return 6
algo
master
.
io
Step:
Start: count every node with a plain DFS
0 / 13
Input
6 Nodes
10 Nodes
Perfect Tree (15)
Single Node
Custom
tree
=
[1, 2, 3, 4, 5, 6]
0 / 13
algo
master
.
io
Step:
Start: count every node with a plain DFS