> ## 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 Univalue Subtrees Python Solution

> Tested Python solution for LeetCode 250 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 250, [Medium](/catalog/medium). Topics: [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/count-univalue-subtrees/description/).

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

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

## Problem

Given the `root` of a binary tree, return *the number of **uni-value*** *subtrees*.

A **uni-value subtree** means all nodes of the subtree have the same value.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0200-0299/0250.Count%20Univalue%20Subtrees/images/unival_e1.jpg)

```
Input: root = [5,1,5,5,5,null,5]
Output: 4
```

```
Input: root = []
Output: 0
```

```
Input: root = [5,5,5,5,5,null,5]
Output: 6
```

### Constraints

* The number of nodes in the tree will be in the range `[0, 1000]`.
* `-1000 <= Node.val <= 1000`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_univalue_subtrees/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_univalue_subtrees/test_solution.py):

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


class Solution:
    # Time: O(n)
    # Space: O(n)
    def count_unival_subtrees(self, root: TreeNode[int] | None) -> int:
        count = 0

        def is_unival(node: TreeNode[int] | None) -> bool:
            nonlocal count
            if node is None:
                return True
            left_unival = is_unival(node.left)
            right_unival = is_unival(node.right)
            if not left_unival or not right_unival:
                return False
            if node.left is not None and node.left.val != node.val:
                return False
            if node.right is not None and node.right.val != node.val:
                return False
            count += 1
            return True

        is_unival(root)
        return count
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.