Problem
You get the root root of a binary tree. In a binary tree, each node has at most two children: a left child and a right child.
A node is good when no node on the path from the root to it has a larger value. The path includes the root and the node itself. So the root is always good.
Return the number of good nodes in the tree.
The examples show each tree as a list in level order. This list gives the nodes row by row, from left to right. The value null marks a missing child.
Example 1
- Input:
- root = [5,3,8,6,1,null,9]
- Output:
- 4
- Explanation:
- The good nodes are
5,8,6and9. Node3and node1are not good, because the root5is larger.
Example 2
- Input:
- root = [4,4,null,2]
- Output:
- 2
- Explanation:
- The second
4is good, because no value on its path is larger. Node2is not good.
Example 3
- Input:
- root = [-1]
- Output:
- 1
Constraints
- 1 <= number of nodes <= 10^5
- -10^4 <= Node.val <= 10^4
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- All Nodes Distance K in Binary Tree
Coding · Binary Tree · Breadth-First Search · Depth-First Search
- Binary Tree Nodes
Coding
- Closest Nodes Queries in a Binary Search Tree
Coding
- Count Complete Tree Nodes
Coding
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
Meta
Leetcode 1448. Count Good Nodes in Binary Tree
Reported Dec 13, 2025 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source