> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Count Good Nodes in Binary Tree

> Tested Python solution for LeetCode 1448 with 17 pytest cases. Generate a practice environment with lcpy.

LeetCode 1448, Medium. Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree. [View on LeetCode](https://leetcode.com/problems/count-good-nodes-in-binary-tree/description/).

Generate this problem as a practice environment: tested reference solution, 17 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1448   # by problem number
lcpy gen -s count_good_nodes_in_binary_tree   # by problem name
```

## 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](https://assets.leetcode.com/uploads/2020/04/02/test_sample_1.png)

```
Input: root = [3,1,4,3,null,1,5]
Output: 4
Explanation: Nodes in blue are good.
Root Node (3) is always a good node.
Node 4 -> (3,4) is the maximum value in the path starting from the root.
Node 5 -> (3,4,5) is the maximum value in the path.
Node 3 -> (3,1,3) is the maximum value in the path.
```

![Example 2](https://assets.leetcode.com/uploads/2020/04/02/test_sample_2.png)

```
Input: root = [3,3,null,4,2]
Output: 3
Explanation: Node 2 -> (3,3,2) is not good, because "3" is higher than it.
```

```
Input: root = [1]
Output: 1
Explanation: Root is considered as good.
```

### 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_good_nodes_in_binary_tree/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_good_nodes_in_binary_tree/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(n)
    # Space: O(h) - recursion stack, h = tree height
    def good_nodes(self, root: TreeNode[int] | None) -> int:
        if root is None:
            return 0

        def dfs(node: TreeNode[int], max_so_far: int) -> int:
            good = 1 if node.val >= max_so_far else 0
            next_max = max(max_so_far, node.val)
            total = good
            if node.left is not None:
                total += dfs(node.left, next_max)
            if node.right is not None:
                total += dfs(node.right, next_max)
            return total

        return dfs(root, root.val)
```

## Complexity

| Time | Space                                   |
| ---- | --------------------------------------- |
| O(n) | O(h) - recursion stack, h = tree height |

## Tags

[NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
