题目地址: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:

  1. The range of tree node value is in the range of [-100000, 100000].
  2. 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++)的更多相关文章

  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. 663. Equal Tree Partition 能否把树均分为求和相等的两半

    [抄题]: Given a binary tree with n nodes, your task is to check if it's possible to partition the tree ...

  3. LeetCode: Binary Search Tree Iterator 解题报告

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  4. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  5. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  6. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  7. 【LeetCode】385. Mini Parser 解题报告(Python)

    [LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...

  8. 【LeetCode】809. Expressive Words 解题报告(Python)

    [LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  9. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

随机推荐

  1. Redis的六种特性 场景

    Redis的六种特性 l Strings l Hashs l Lists l Sets l Sorted Sets l Pub/Sub Redis各特性的应用场景 Strings Strings 数据 ...

  2. Spark(十二)【SparkSql中数据读取和保存】

    一. 读取和保存说明 SparkSQL提供了通用的保存数据和数据加载的方式,还提供了专用的方式 读取:通用和专用 保存 保存有四种模式: 默认: error : 输出目录存在就报错 append: 向 ...

  3. set、multiset深度探索

    set/multiset的底层是rb_tree,因此它有自动排序特性.set中的元素不允许重复必须独一无二,key与value值相同,multiset中的元素允许重复. set的模板参数key即为关键 ...

  4. Linux下强制踢掉登陆用户

    1.pkill -kill -t   tty 例:pkill -kill -t tty1

  5. Default Constructors

    A constructor without any arguments or with default value for every argument, is said to be default ...

  6. springboot+vue集成mavon-editor,开发在线文档知识库

    先睹为快,来看下效果: 技术选型 SpringBoot.Spring Security.Oauth2.Vue-element-admin 集成mavon-editor编辑器 安装 mavon-edit ...

  7. java关键字volatile内存语义详细分析

    volatile变量自身具有下列特性. 1.可见性.对一个volatile变量的读,总是能看到(任意线程)对这个volatile变量最后的写 入. · 2.原子性:对任意单个volatile变量的读/ ...

  8. pandas基础学习一

    生成对象 用值列表生成 Series 时,Pandas 默认自动生成整数索引: In [3]: s = pd.Series([1, 3, 5, np.nan, 6, 8]) In [4]: s Out ...

  9. 【JS】枚举类型

    https://zhuanlan.zhihu.com/p/79137838 相当于用数字来代替一串字母 /** * 时间:2019年8月18日 * 前端教程: https://www.pipipi.n ...

  10. 【科研工具】流程图软件Visio Pro 2019 详细安装破解教程

    [更新区] 安装教程我下周会在bilibili上传视频,这周事情太多暂时先不弄. [注意] 安装Visio需要和自己的Word版本一样,这里因为我的Word是学校的正版2019(所以学校为什么正版没买 ...