您的位置:首页 > 产品设计 > UI/UE

Count Univalue Subtrees

2016-06-23 14:06 393 查看
参考:点击打开链接

/**
* 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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: