[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;
    }
}
时间: 2025-01-11 09:53:34

[LeetCode#250] Count Univalue Subtrees的相关文章

[LeetCode] 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. 给一个二叉树,求唯一值子树的个数.唯一值子树的所有节点具有相同值. 解法:递归 Java: /** * Defini

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. 链接: http://leetcode.com/problems/count-univalue-subtre

[LC] 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. Example : Input: root = [5,1,5,5,5,null,5] 5 / 1 5 / \ 5 5 5 Output: 4 /** * Definition for a binary tree node. * pub

[Locked] Count Univalue Subtrees

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. 分析: 有点像自低向上的动态规划,既然是自底向上,看来用递归访问树的

LeetCode Count Univalue Subtrees

原题链接在这里:https://leetcode.com/problems/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 retur

[LeetCode] 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. 这道题让我们求相同值子树的个数,就是所有节点值都相同的子树的个数,之前有道求最大BST子树的题Largest BST

[LeetCode] 687. Longest Univalue Path 最长唯一值路径

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them. Ex

LeetCode:Count and Say

1.题目名称 Count and Say(按照数字重复出现计数并生成字符串) 2.题目地址 https://leetcode.com/problems/count-and-say/ 3.题目内容 英文:The count-and-say sequence is the sequence of integers beginning as follows 中文:给出正整数n,返回"count-and-say"序列的第n项 说明: count-and-say序列形如:1, 11, 21, 1

LeetCode:Count Primes - 统计质数数量

1.题目名称 Count Primes(统计质数数量) 2.题目地址 https://leetcode.com/problems/count-primes/ 3.题目内容 英文:Count the number of prime numbers less than a non-negative number, n. 中文:统计正整数n以内(不含n本身)质数的数量 4.一个TLE的方法 从1到n,考察每个数字是否为质数.这个方法由于花费时间较长,不能满足题目中对时间的要求. 一段实现此方法的Jav