Skip to main content
LeetCode 1448, Medium. Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 17 parametrized pytest cases, and a playground notebook:

Problem

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.

Examples

Example 1
Example 2

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].

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode 150, NeetCode 250, NeetCode All.
Last modified on August 25, 2026