# Problem 250: Count Univalue Subtrees

> <https://leetcode.com/problems/count-univalue-subtrees/>

![](/files/-Lpv9wCv-c3eF6oa5QlX)

## 思路

* subtree 一样，是说 node 的值和它左右子树的值都想同
* 那我们如何统计所有的结点呢？可以 preorder 遍历所有的结点，然后对于每一个结点，判断它的孩子们是否和它的值都相同，这样就可以没有遗漏也不重复地扫过所有的 nodes 了。
* 对于判断每一个 node 的时候，用一个 flag 来标记它是否是唯一的。然后递归所有的子树。

```java
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int countUnivalSubtrees(TreeNode root) {
        if (root == null) return 0;

        int count = isUni(root) ? 1 : 0;
        return count + countUnivalSubtrees(root.left) + countUnivalSubtrees(root.right);
    }

    private boolean isUni(TreeNode root) {
        boolean flag = true;
        if (root.left != null) {
            flag &= (root.val == root.left.val);
            flag &= isUni(root.left);
        }
        if (root.right != null) {
            flag &= (root.val == root.right.val);
            flag &= isUni(root.right);
        }

        return flag;
    }
}
```


---

# 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://liuyang89116.gitbook.io/my-leetcode-book/chapter_3_binary_tree_bfs-_dfs/problem-250-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.
