Back to course home
0% completed
Vote For New Content
Count Good Nodes in Binary Tree (medium)
Problem Statement
Given a binary tree root
, return the number of good
nodes in the binary tree.
A node is considered good
if in the path from the root to this node, there are no nodes with a value greater than
this node's value.
Examples
Example 1
- Input: root =
[3, 1, 3, 3, null, 1, 5]
- Expected Output:
4
- Explanation:
- Root node (3) is always good.
- Node
5
is also a good node. - Both nodes
3
are good.
Example 2
- Input: root =
[2, 3, 4, 1, null, null, 5]
- Expected Output:
4
- Explanation:
- Nodes
2
,3
,4
, and5
are good. - The node with value
1
is not good because3
is greater and comes before it.
- Nodes
Example 3
- Input: root =
[10, 8, 15, 7, 9, 12, 20]
- Expected Output:
3
- Explanation:
- Nodes
10
,15
, and20
are good.
- Nodes
Constraints:
- The number of nodes in the binary tree is in the range [1, 10^<sup>5</sup>].
- Each node's value is between [-10<sup>4</sup>, 10^<sup>4</sup>].
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
On this page
Problem Statement
Examples
Try it yourself