# 250. Count Univalue Subtrees

[250. Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees/)

```python
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isUniValSubStree(self, node: TreeNode) -> bool:
        if node.right is None and node.left is None:
            self.count += 1
            return True
        is_uni = True
        if node.left:
            is_uni = self.isUniValSubStree(node.left) and is_uni and  node.left.val == node.val
        if node.right:
            is_uni = self.isUniValSubStree(node.right) and is_uni and  node.right.val == node.val
        if is_uni:
            self.count += 1
        return is_uni

    def countUnivalSubtrees(self, root: TreeNode) -> int:
        if not root:
            return 0
        self.count = 0
        self.isUniValSubStree(root)
        return self.count
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://garylai.gitbook.io/algorithm-and-data-structure/problems/tree/count-univalue-subtrees.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
