[LeetCode#250] Count Univalue Subtrees

Problem:

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.

Analysis:

This problem is super simple. 
But, because my poor mastery of Java language, I have committed following simple mistakes. 

1. use bit wise operator as and operator.
is_unival & = (root.val == root.left.val);
Line 26: error: illegal start of expression


2. Did not consider Java would automatically optimize the running of code.
For logic operator : and ("&&")
In fact:
C = A && B
If A is false, Java would not execute B. (Thus B must be a primitive value!!! Rather than any function call).
----------------------------------------------------------------------------------------------------------
    private boolean helper(TreeNode root, List<Integer> ret) {
        if (root == null)
            return true;
        boolean is_unival = true;
        if (root.left != null) 
            is_unival = is_unival && (root.val == root.left.val);
        if (root.right != null)
            is_unival = is_unival && (root.val == root.right.val);
        is_unival = is_unival && helper(root.left, ret);
        is_unival = is_unival && helper(root.right, ret);
        if (is_unival)
            ret.set(0, ret.get(0)+1);
        return is_unival;
    }
----------------------------------------------------------------------------------------------------------
Error output:
Input:
[5,1,5,5,5,null,5]
Output:
0
Expected:
4

If is_unival is false before reach 
is_unival = is_unival && helper(root.left, ret);
is_unival = is_unival && helper(root.right, ret);

Both "helper(root.left, ret)" and "helper(root.right, ret)", would not be executed.
So as to write code as
flag = (helper(root.left, ret) && helper(root.right, ret)). 
The above code would cause the problem. The way to avoid such case is to write each statement seprately and use flag_n to hold the temp result.

boolean flag1 = true, flag2 = true, flag3 = true, flag4 = true;
if (root.left != null) 
    flag1 = (root.val == root.left.val);
if (root.right != null)
    flag2 = (root.val == root.right.val);
flag3 = helper(root.left, ret);
flag4 = helper(root.right, ret);
is_unival = flag1 && flag2 && flag3 && flag4;

Note: For conjunction equation, the initial value for each element should be false rather than true.

Solution:

public class Solution {
    public int countUnivalSubtrees(TreeNode root) {
        if (root == null)
            return 0;
        List<Integer> ret = new ArrayList<Integer> ();
        ret.add(0);
        helper(root, ret);
        return ret.get(0);
    }
    
    
    private boolean helper(TreeNode root, List<Integer> ret) {
        if (root == null)
            return true;
        boolean is_unival = true;
        boolean flag1 = true, flag2 = true, flag3 = true, flag4 = true;
        if (root.left != null) 
            flag1 = (root.val == root.left.val);
        if (root.right != null)
            flag2 = (root.val == root.right.val);
        flag3 = helper(root.left, ret);
        flag4 = helper(root.right, ret);
        is_unival = flag1 && flag2 && flag3 && flag4;
        if (is_unival)
            ret.set(0, ret.get(0)+1);
        return is_unival;
    }
}

 

posted @ 2015-09-14 04:37  airforce  阅读(505)  评论(0编辑  收藏  举报