Given a binary tree, count the number of uni-value subtrees.
A Uni-value subtree means all nodes of the subtree have the same value.
Example :
Input: root = [5,1,5,5,5,null,5] 5 / \ 1 5 / \ \ 5 5 5 Output: 4
Related Topics:
Tree
Similar Questions:
// OJ: https://leetcode.com/problems/count-univalue-subtrees/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(H) where H is the height of the tree
class Solution {
private:
int cnt = 0;
bool postorder(TreeNode *root) {
if (!root) return true;
bool left = postorder(root->left), right = postorder(root->right);
if (left && right
&& (!root->left || root->val == root->left->val)
&& (!root->right || root->val == root->right->val)) {
++cnt;
return true;
}
return false;
}
public:
int countUnivalSubtrees(TreeNode* root) {
postorder(root);
return cnt;
}
};