参考:点击打开链接
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int count = 0;
public int countUnivalSubtrees(TreeNode root) {
helper(root);
return count;
}
private boolean helper(TreeNode root) {
if (root == null) {
return true;
}
boolean left = helper(root.left);
boolean right = helper(root.right);
///if (left && right && (left == null || left.val == root.val) && (right == null || right.val == root.val)) {
if (left && right && (root.left == null || root.left.val == root.val) && (root.right == null || root.right.val == root.val)) {
count++;
return true;
}
return false;
}
}