【LeetCode】663. Equal Tree Partition 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/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 one edge on the tree.
Note:
- The range of tree node value is in the range of [-100000, 100000].
- 1 <= n <= 10000
题目大意
删除二叉树中的一条边,能否使得剩余的两个树的节点之和相等?
解题方法
递归
这个题就是分割二叉树,使得分割完之后两部分和相等。那么,所有节点总的和必须是偶数。我们可以求出总和total,判断total/2是否是其中的一个子树,如果是的话就可以分割出来。
使用递归的做法,计算出每个子树的所有节点之和。这样也就知道了整个树的所有节点之和total,此total必须是偶数。然后找half = total / 2是否是某个子树
的和。
为了防止重复计算子树的和,使用了字典保存以每个节点为根的子树的和。这样已经计算的节点可以直接查表得到其子树和。
C++代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool checkEqualTree(TreeNode* root) {
int total = getSum(root);
if (total & 1)
return false;
int half = total / 2;
return subEqual(root->left, half) || subEqual(root->right, half);
}
bool subEqual(TreeNode* root, int target) {
if (!root) return false;
if (getSum(root) == target)
return true;
return subEqual(root->left, target) || subEqual(root->right, target);
}
int getSum(TreeNode* root) {
if (!root) return 0;
if (m.count(root))
return m[root];
int res = root->val + getSum(root->left) + getSum(root->right);
m[root] = res;
return res;
}
private:
unordered_map<TreeNode*, int> m;
};
参考资料:https://leetcode-cn.com/problems/equal-tree-partition/solution/java-di-gui-by-zxy0917-16/
日期
2019 年 9 月 21 日 —— 莫生气,我若气病谁如意
【LeetCode】663. Equal Tree Partition 解题报告 (C++)的更多相关文章
- [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 ...
- 663. Equal Tree Partition 能否把树均分为求和相等的两半
[抄题]: Given a binary tree with n nodes, your task is to check if it's possible to partition the tree ...
- LeetCode: Binary Search Tree Iterator 解题报告
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】385. Mini Parser 解题报告(Python)
[LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...
- 【LeetCode】809. Expressive Words 解题报告(Python)
[LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
随机推荐
- 金蝶EAS——我的EAS报销流程怎么能让另一个人看到呢?即如何设置流程传阅功能?设置“代理报销”
代理的话只能看到被代理人能看到的流程.设置"代理报销":应用--财务会计--费用管理--代理报销 选择报销人公司--"他人代理我报销"--选择报销人(zhaof ...
- 自动添加shell脚本头部信息
autocmd BufNewFile *.sh exec ":call AddTitleForShell()" function AddTitleForShell() call a ...
- KEGG通路图应该怎么看(转载)
转载:http://www.omicshare.com/forum/thread-107-1-3219.html (出处: OmicShare Forum) 不管是RNA-seq的分析数据,还是蛋白组 ...
- 使用Mybatis出现的问题+配置优化+ResultMap
一.可能出现的问题 1.Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: ...
- 学习java的第二十七天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- 【Linux】【Services】【VersionControl】Git基础概念及使用
1. 简介 1.1. 版本控制工具: 本地版本控制系统: 集中化版本控制系统:CVS,SVN 分布式版本控制系统: BitKeeper,Git 1.2. 官方网站: https://git-scm.c ...
- ORACLE 数据块PCTFREE和PCTUSED
PCTFREE表示一个数据块可用空间小于PCTFREE时,该数据块不在被记录在FREELIST中,即不能插入新数据. PCTUSED表示一个数据块已经用空间如果小于PCTUSED时,该数据块才会被重新 ...
- Linux基础命令----smbclient
smbclient smbclient是一个smb服务器的客户端的管理程序,可以交互式的访问samba服务器. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.SU ...
- 使用fastDFS上传和下载图片文件
package com.xuecheng.test.fastdfs;import org.csource.common.MyException;import org.csource.fastdfs.* ...
- 1.RabbitMQ
1.RabbitMq是什么? MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发,基于AMQP(Advanced Message Queue 高级消息队 ...