---
tags: leetcode
---
# [250. Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees/)
---
# My Solution
## Solution 1
### The Key Idea for Solving This Coding Question
### C++ Code 1
```cpp=
class Solution {
public:
int countUnivalSubtrees(TreeNode *root) {
int cnt = 0;
dfs(nullptr, root, cnt);
return cnt;
}
private:
// dfs returns true if root and parent is a univalue subtree.
// Otherwise, reutrns false.
bool dfs(TreeNode *parent, TreeNode *root, int &cnt) {
if (root == nullptr) {
// root's parent could be a leaf.
return true;
}
bool isLeft = dfs(root, root->left, cnt);
bool isRight = dfs(root, root->right, cnt);
if (isLeft && isRight) {
++cnt;
if (parent != nullptr) {
return parent->val == root->val;
}
// Because parent is nullptr, root is the root of the
// whole tree.
return true;
}
return false;
}
};
```
### C++ Code 2
```cpp=
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int countUnivalSubtrees(TreeNode *root) {
cnt = 0;
dfs(root, INT_MIN);
return cnt;
}
private:
int cnt;
// dfs returns true if root and its parent is a univalue subtree.
// Otherwise, return false.
bool dfs(TreeNode *root, int parentVal) {
if (root == nullptr) {
return true;
}
bool isLeftUniValue = dfs(root->left, root->val);
bool isRightUniValue = dfs(root->right, root->val);
if (isLeftUniValue && isRightUniValue) {
++cnt;
return root->val == parentVal;
}
return false;
}
};
```
### Time Complexity
$O(n)$
$n$ is the number of nodes in the binary tree referred by `root`.
### Space Complexity
$O(H)$
$H$ is the height of the binary tree referred by `root`.
## Solution 2
### The Key Idea for Solving This Coding Question
### C++ Code
```cpp=
class Solution {
public:
int countUnivalSubtrees(TreeNode *root) {
cnt = 0;
dfs(root);
return cnt;
}
private:
int cnt;
void dfs(TreeNode *root) {
if (root == nullptr) {
return;
}
dfs(root->left);
dfs(root->right);
if (isUnivalue(root)) {
++cnt;
}
}
bool isUnivalue (TreeNode *root) {
if (root == nullptr) {
return true;
}
if (root->left == nullptr && root->right == nullptr) {
return true;
}
if (root->left == nullptr) {
return root->val == root->right->val && isUnivalue(root->right);
}
if (root->right == nullptr) {
return root->val == root->left->val && isUnivalue(root->left);
}
return root->val == root->left->val && root->val == root->right->val &&
isUnivalue(root->left) && isUnivalue(root->right);
}
};
```
### Time Complexity
$O(n)$
$n$ is the number of nodes in the binary tree referred by `root`.
### Space Complexity
$O(H)$
$H$ is the height of the binary tree referred by `root`.
# Miscellane
<!--
# Test Cases
```
[5,1,5,5,5,null,5]
```
```
[]
```
```
[5,5,5,5,5,null,5]
```
* Runtime Error:
```
[1,null,2]
```
* Wrong Answer:
```
[1,1,1,5,5,null,5]
```
* Wrong Answer:
```
[5,5,1]
```
* Wrong Answer
```
[1,74,74,68,-76,-76,68,-32,null,null,-85,null,-85,null,-32,null,-69,53,-80,-80,53,-69,null,null,96,-73,null,62,null,null,62,null,-73,96,null,76,null,null,-49,null,-14,-14,null,-49,null,null,76,11,null,-93,null,null,98,98,null,null,-93,11,null,43,null,-60,-75,-58,46,46,-58,-75,-60,null,43,69,null,null,15,null,-12,null,-69,-54,null,null,-54,-69,null,-12,null,15,null,null,69,-35,null,31,null,null,-57,null,99,-85,null,null,-85,99,null,-57,null,null,31,null,-35,null,78,null,-88,-87,18,58,null,71,15,15,71,null,58,18,-87,-88,null,78,null,-99,-3,null,-17,null,-38,-86,null,null,45,null,-76,null,-30,-30,null,-76,null,45,null,null,-86,-38,null,-17,null,-3,-99,24,null,54,null,null,62,-83,null,1,null,20,null,null,42,-12,null,null,-12,42,null,null,20,null,1,null,-83,62,null,null,54,null,24,94,null,null,75,-81,null,-69,null,81,38,null,52,56,null,95,94,94,95,null,56,52,null,38,81,null,-69,null,-81,75,null,null,94,null,95,-95,null,-13,null,32,null,80,null,-50,null,null,-68,null,51,null,5,-13,null,null,-13,5,null,51,null,-68,null,null,-50,null,80,null,32,null,-13,null,-95,95,null,null,12,null,77,13,null,null,-90,null,-92,null,59,20,null,-11,-12,-98,null,-47,null,null,-47,null,-98,-12,-11,null,20,59,null,-92,null,-90,null,null,13,77,null,12]
```
-->