Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Count Good Nodes in Binary Tree
Bookmark
DFS Full Path
DFS Max Tracking
BFS Max Tracking
Input
Example 1 (count=4)
Example 2 (count=3)
Single Node (count=1)
Custom
tree
=
[3, 1, 4, 3, null, 1, 5]
path
3
1
4
3
1
5
good
0
good node: >= every value on its stored root path
path
3
1
4
3
1
5
good
0
good node: >= every value on its stored root path
path
3
3
1
4
3
1
5
+1
good
1
recurse left
path
3
1
3
3
1
4
3
1
5
+1
+0
+1
good
2
3
is the path max: good, count =
2
path
3
1
3
0
3
1
4
3
1
5
+1
+0
+1
good
2
null child: contributes
0
path
3
3
1
4
3
1
5
+1
+0
+1
good
2
return
0
+
1
+
0
=
1
path
3
4
1
3
1
4
3
1
5
+1
+0
+1
+1
good
3
push
1
: path max =
4
path
3
4
1
3
1
4
3
1
5
+1
+0
+1
+1
+0
good
3
right is null: contributes
0
path
3
4
5
3
1
4
3
1
5
+1
+0
+1
+1
+0
good
3
push
5
: path max =
5
path
3
4
5
0
3
1
4
3
1
5
+1
+0
+1
+1
+0
+1
good
4
null child: contributes
0
path
3
1
4
3
1
5
+1
+0
+1
+1
+0
+1
good
4
return 4
algo
master
.
io
Step:
Start: a node is good if it is >= every value on its root path
0 / 39
Input
Example 1 (count=4)
Example 2 (count=3)
Single Node (count=1)
Custom
tree
=
[3, 1, 4, 3, null, 1, 5]
0 / 39
algo
master
.
io
Step:
Start: a node is good if it is >= every value on its root path