这是一道谷歌会考的题目
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
解法一
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int countUnivalSubtrees(TreeNode root) {
if(root==null) return 0;
int totalCount = countUnivalSubtrees(root.left)+countUnivalSubtrees(root.right);
if(isUni(root)) totalCount+=1;
return totalCount;
}
public boolean isUni(TreeNode root){
if(root==n