Skip to content

Practice Count Good Nodes in Binary Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1448
LeetCode Problem

Count Good Nodes in Binary Tree

Problem Statement

Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X. Return the number of good nodes in the binary tree.

Source: Count Good Nodes in Binary Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
root = [3,1,4,3,null,1,5]
Output
4

# Example 2

Input
root = [3,3,null,4,2]
Output
3

# Example 3

Input
root = [1]
Output
1

Constraints

The number of nodes in the binary tree is in the range [1, 10^5].
Each node's value is between [-10^4, 10^4].

Related LeetCode Problems

Practice more problems similar to Count Good Nodes in Binary Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.