• [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;
        }
    }
  • 相关阅读:
    Apache-Shiro分布式环境配置(与redis集成)(转)
    springboot整合mybatis将sql打印到日志(转)
    springboot中logback打印日志(转)
    Spring Boot Junit单元测试
    玩转Spring Boot 自定义配置、导入XML配置与外部化配置
    Windows开机自启动位置
    木马开机启动的六种方法(记录)
    用Delphi开发视频聊天软件
    Delphi用Socket API实现路由追踪
    前端工程师应该都了解的16个最受欢迎的CSS框架
  • 原文地址:https://www.cnblogs.com/airwindow/p/4806087.html
Copyright © 2020-2023  润新知