250. Count Univalue Subtrees

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.

For example:
Given binary tree,

              5
             / \
            1   5
           / \   \
          5   5   5

return 4.

This is a very good example to show different solutions to the tree problem.


Solution one, divide and conquer

Compute the result in the left subtree and right subtree. add them together, if the root.val is same to the all left value and all right value, add another one.

The problem is even if the root.val == root.left.val && root.val == root.right.val, we dont know whether all the node in the subtree have the same value, so we need to create a Result type to return mutiple types of result, the boolean specifies if all the nodes in the subtree are same, and the num states the number of univalue Subtree.

Code:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    class Result{
        boolean allSame;
        int num;
        public Result(boolean allSame, int num){
            this.allSame = allSame;
            this.num = num;
        }
    }
    
    
    public int countUnivalSubtrees(TreeNode root) {
        return helper(root).num;
    }
    
    
    public Result helper(TreeNode root){
        if(root == null) return new Result(true,0);
        Result left = helper(root.left);
        Result right = helper(root.right);
        int num = left.num + right.num;
        
        if(left.allSame && right.allSame 
            && (root.left == null ? true : root.left.val == root.val)
            && (root.right == null ? true : root.right.val == root.val)){
            return new Result(true, num + 1);        
        }
        
        return new Result(false,num);
            
    }
}

Second Solution:  Traverse and add.

We need to traverse to each node and count bottom up, we maintain a global varible to count.

public class Solution {
    int count;
    
    public int countUnivalSubtrees(TreeNode root) {
        helper(root);
        return count;
    }
    
    
    boolean helper(TreeNode root){
        if(root == null) return true;
        if(root.left == null && root.right == null){
            count += 1;
            return true;
        }
        
        boolean left = helper(root.left);
        boolean right = helper(root.right);
        
        if(left && right 
                && (root.left == null ? true : root.left.val == root.val)
                && (root.right == null ? true : root.right.val == root.val)){
            count += 1;
            return true;
        }
        return false;
    }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值