zoukankan      html  css  js  c++  java
  • 663. Equal Tree Partition 能否把树均分为求和相等的两半

    [抄题]:

    Given a binary tree with n nodes, your task is to check if it's possible to partition the tree to two trees which have the equal sum of values after removing exactly one edge on the original tree.

    Example 1:

    Input:     
        5
       / 
      10 10
        /  
       2   3
    
    Output: True
    Explanation: 
        5
       / 
      10
          
    Sum: 15
    
       10
      /  
     2    3
    
    Sum: 15
    

     

    Example 2:

    Input:     
        1
       / 
      2  10
        /  
       2   20
    
    Output: False
    Explanation: You can't split the tree into two trees with equal sum after removing exactly on

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    以为是传统的dc-路径求和,没想到是分成几个函数来做。完全没想到。

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [一句话思路]:

    getTotal(root.left) + getTotal(root.right) + root.val通过自定义的函数名来进行traverse

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. 先退出再expand, checkequal只要有一个true就直接退出了

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    通过自定义的函数名来进行traverse,先退出再expand, checkequal只要有一个true就直接退出了 

    [复杂度]:Time complexity: O(n) Space complexity: O(1)

    [算法思想:迭代/递归/分治/贪心]:

    [关键模板化代码]:

    public long checkEqual(TreeNode root) {
            //exit or expand
            if (root == null || equal) return 0;
            
            long curSum = checkEqual(root.left) + checkEqual(root.right) + root.val;
            if (curSum == targetSum - curSum) {
                equal = true;
                return 0;
            }
            return curSum;
        }

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

     [潜台词] :

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        //initialiazation
        boolean equal = false;
        long targetSum = 0;
        
        public boolean checkEqualTree(TreeNode root) {
            targetSum = getSum(root);
            checkEqual(root);
            return equal;
        }
        
        public long getSum(TreeNode root) {
            //exit or expand
            if (root == null) return 0;
            return getSum(root.left) + getSum(root.right) + root.val;
        }
        
        public long checkEqual(TreeNode root) {
            //exit or expand
            if (root == null || equal) return 0;
            
            long curSum = checkEqual(root.left) + checkEqual(root.right) + root.val;
            if (curSum == targetSum - curSum) {
                equal = true;
                return 0;
            }
            return curSum;
        }
    }
    View Code
  • 相关阅读:
    vue中dom元素和组件的获取
    Vue.js中父子组件之间的传值和传方法
    IDEA中的快捷键
    springmvc中使用controller时,跳转视图会带上外层的地址
    通配符的匹配很全面, 但无法找到元素 'mvc:annotation-driven' 的声明
    vue中的组件
    vuejs
    成员变量(实例变量)&局部变量&静态变量(类变量)的区别
    代码块
    重载&重写
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9612564.html
Copyright © 2011-2022 走看看