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 one edge on the tree.

Note:

  1. The range of tree node value is in the range of [-100000, 100000].
  2. 1 <= n <= 10000

这道题让我们划分等价树,就是说当移除一条边后,被分成的两棵树的结点之和需要相等。通过观察题目中的例子可以发现,如果将每个结点的结点值变成其所有子结点的结点值之和再加上当前的结点值,那么对于例子1来说,根结点的结点值就变成了 30,断开位置的结点就变成了 15,可以发现其实只要断开位置的结点值是根结点值的一半,就存在等价划分。所以这道题的难点就是更新每个结点的结点值,可以使用递归来做。博主最开始使用的是 unordered_set,把更新后的每个结点值都存入集合中,但是对于 test case: [0, 1, -1] 会 fail, 仔细分析下这个 case,发现更新后的根结点值还是0,而且0已经被存入集合了,而0除以2还是0,在集合中存在,会返回 true,但其实这棵树是不能等价划分的。0的情况确实比较特殊,所以这里要使用 HashMap,建立更新后的结点值和其出现次数之间的映射,这样只有 HashMap 中0的个数大于1的时候,才返回 true。这样完美的避开了根结点为0的陷阱,Perfect!参见代码如下:

解法一:

class Solution {
public:
bool checkEqualTree(TreeNode* root) {
unordered_map<int, int> m;
int sum = helper(root, m);
if (sum == ) return m[] > ;
return (sum % == ) && m.count(sum / );
}
int helper(TreeNode* node, unordered_map<int, int>& m) {
if (!node) return ;
int cur = node->val + helper(node->left, m) + helper(node->right, m);
++m[cur];
return cur;
}
};

我们也可以使用 stack 来做,将所有的结点和存入到栈中,然后依次出栈,看是否有结点和正好等于 sum/2,这里还是要注意当 sum=0 的情况同时根结点值又正好是0的情况,最简单的处理办法就是将栈顶元素提前移除,这样就不会有 sum=sum/2 这种情况发生了,参见代码如下:

解法二:

class Solution {
public:
bool checkEqualTree(TreeNode* root) {
stack<int> st;
int sum = helper(root, st);
st.pop();
if (sum % != ) return false;
while (!st.empty()) {
if (st.top() == sum / ) return true;
st.pop();
}
return false;
}
int helper(TreeNode* node, stack<int>& st) {
if (!node) return ;
st.push(helper(node->left, st) + helper(node->right, st) + node->val);
return st.top();
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/663

参考资料:

https://leetcode.com/problems/equal-tree-partition/

https://leetcode.com/problems/equal-tree-partition/discuss/149437/Logical-Thinking-with-Java-Code-Beats-97.25

https://leetcode.com/problems/equal-tree-partition/discuss/106727/JavaC%2B%2B-Simple-solution-with-only-one-HashMaplessgreater.

LeetCode All in One 题目讲解汇总(持续更新中...)

最新文章

  1. JavaSe:Properties文件格式
  2. SQL Server中的窗口函数
  3. ssh框架整合-NoClassDefFoundError-NoSuchMethodError-遁地龙卷风
  4. 【BZOJ】3526: [Poi2014]Card
  5. zw.delphi不同版本程序运行速度测试
  6. Ansible 学习笔记
  7. 解决Android SDK Manager 更新、下载慢以及待安装包列表不显示
  8. 转--Android实现ListView过滤功能,继承于BaseAdapter,非ArrayAdapter。
  9. 响应式Asp.net MVC企业网站源码
  10. 我用的php开发环境是appserv一键安装,通过http://localhost测试成功,但是我有点不清楚的就是为什么访问.php文件要在地址栏上加上localhost(即http://localhost/text.php)才能成功访问?
  11. JavaScript------for-in的使用方法
  12. Java数据结构和算法(六)——前缀、中缀、后缀表达式
  13. 下载android5.0源码
  14. js把变量转换成json数据
  15. VSCode git Warning LF will be replaced by CRLF
  16. Tomcat 7.0安装与配置
  17. Centos6下给PHP安装Qconf扩展
  18. iOS性能优化总结
  19. ui设计教程分享:关于Logo设计要素
  20. shopify网站转化率优化之结账页checkout优化

热门文章

  1. antd模块组件文档思维导图整理
  2. 响应国家号召 1+X 证书 Web 前端开发考试模拟题
  3. ASCll编码,
  4. WPF 通过EventTrigger修改鼠标样式
  5. ML.NET调用Tensorflow模型示例——MNIST
  6. MySQL出现Waiting for table metadata lock的原因以及解决方法(转)
  7. Winforn中设置ZedGraoh的GraphPane恢复到初始比例大小
  8. Visual Studio 2017使用ODT 连接Oracle 数据库出现异常
  9. jieba分词原理-DAG(NO HMM)
  10. Docker Desktop for Windows 安装步骤