[抄题]:

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;
}
}

663. Equal Tree Partition 能否把树均分为求和相等的两半的更多相关文章

  1. [LeetCode] 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 tw ...

  2. [LeetCode] Equal Tree Partition 划分等价树

    Given a binary tree with n nodes, your task is to check if it's possible to partition the tree to tw ...

  3. 【LeetCode】663. Equal Tree Partition 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  4. [BZOJ3754]Tree之最小方差树

    3754: Tree之最小方差树 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 402  Solved: 152[Submit][Status][Di ...

  5. [BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树

    [BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树 题目大意: 给定一个\(n(n\le50)\)个点,\(m(m\le1000 ...

  6. poj3321-Apple Tree(DFS序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36442   Accepted: 10894 Desc ...

  7. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  8. CSU 1811: Tree Intersection(线段树启发式合并||map启发式合并)

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1811 题意:给出一棵树,每一个结点有一个颜色,然后依次删除树边,问每次删除树边之后,分开的两个 ...

  9. Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式)

    Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式) 题外话,这是我第40篇随笔,纪念一下.<( ̄︶ ̄)↗[GO!] 题意 是说有棵树,每个节点上 ...

随机推荐

  1. ANSYS耦合

    目录 定义 如何生成耦合自由度集 1.在给定节点处生成并修改耦合自由度集 2.耦合重合节点. 3.迫使节点有相同的表现方式 生成更多的耦合集 1. CPLGEN 2.CPSGEN 使用耦合注意事项 约 ...

  2. FPGA 关于中间环节生成时钟的约束---Generated clocks

    FPGA 关于中间环节生成时钟的约束---Generated clocks FPGA 的生成时钟,有一个主时钟源,在定义的之后,需要指明主时钟源. 生成时钟主要定义的是:分频,倍频,相移等 二分频时钟 ...

  3. IIS 7.0的集成模式和经典模式

    IIS7.0中的Web应用程序有两种配置模式:经典模式和集成模式.经典模式是为了与之前的版本兼容,使用ISAPI扩展来调用ASP.NET运行库, 原先运行于IIS6.0下的Web应用程序迁移到IIS7 ...

  4. ActiveMQ-5.15.2下载和启动(windows)

    一.下载和部署 我的ActiveMQ版本是 5.15.2,参照别人家的博客,下载和启动照样成功.别人家的博客地址: http://blog.csdn.net/clj198606061111/artic ...

  5. [UE4]Invalidation Box

    Invalidation Box:使条目无效的容器.使容器内的条目不再更新,如果确定某一个UI不需要更新的话,就可以把这个UI放到Invalidation Box中. 一.Invalidation B ...

  6. AI与RPA

    RPA(机器人流程自动化)是一类自动化软件工具,它可以通过用户界面使用和理解企业已有的应用,将基于规则的常规操作自动化,例如读取邮件和系统,计算,生成文件和报告,检查文件等.因此,RPA的应用范围非常 ...

  7. Error:Execution failed for task :app:transformClassesWithInstantRunForDebug解决方案

    转自https://blog.csdn.net/student9128/article/details/53026990

  8. Linux背背背(4)vim操作

    目录 1.打开文件 2.vim的三种模式 3.扩展 (关于vi 和 vim 的区别,它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面.) 1 ...

  9. linux 配置vue环境

    系统 [root@Gao conf.d]# uname -a 工具 1.Final Shell 2.工具截图 需要下载的部分 node.js    npm   cnpm   vue-cli 安装nod ...

  10. delete content on the right of cursor, Mac

    delete content on the right of cursor, Mac It's not convenient to press Fn+delete to delete content ...