[LeetCode]Count Univalue Subtrees

public class Solution {
    int result = 0;
    public int countUnivalSubtrees(TreeNode root) {
        helper(root);
        return result;
    }
    public boolean helper(TreeNode root) {
        if (root == null) {
            return true;
        }
        boolean left = helper(root.left);
        boolean right = helper(root.right);
        if ((left && (root.left == null || root.val == root.left.val)) && (right && (root.right == null || root.val == root.right.val))) {
            result ++;
            return true;
        }
        return false;
    }
}

 

posted @ 2015-11-29 12:50  Weizheng_Love_Coding  阅读(128)  评论(0编辑  收藏  举报