码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Count Univalue Subtrees

时间:2015-11-29 13:28:15      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }
}

 

[LeetCode]Count Univalue Subtrees

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5004614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!