[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 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
思路:
递归求子树的和,观察子树的和是否是整个树的和的一半即可。
int sumTree(TreeNode* root)
{
if(root==NULL)return ;
int left = sumTree(root->left);
int right = sumTree(root->right);
return root->val+left+right;
} bool bianli(TreeNode* root,int sum)
{
if(root==NULL)return false;
if(sumTree(root->left)*==sum)return true;
if(sumTree(root->right)*==sum)return true;
return (bianli(root->left,sum) || bianli(root->right,sum));
}
bool checkEqualTree(TreeNode* root)
{
if(root==NULL ||(root->left==NULL && root->right==NULL))return false;
int sum = sumTree(root);
if(sum%==)return false;
return bianli(root,sum);
}
[leetcode-663-Equal Tree Partition]的更多相关文章
- [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 ...
- 【LeetCode】663. Equal Tree Partition 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://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 ...
- [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 ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
随机推荐
- ARC下IBOutlet用weak还是strong
原文来自这里. 今天用Xcode5的时候,发现默认的IBoutlet的属性设置为weak——因为Xcode5建立的工程都是ARC的了.但是当时还有点不明白,因为项目的原因,一直没有正式使用过ARC.于 ...
- jQuery实现 自动滚屏操作
实现自动滚屏思路: 1.滚屏即:文本的往上移动一段距离: 2.那么我们使文本每过一段时间就往上移动一段固定距离,就可实现滚屏: 3.直到文本底部出现在浏览器窗口中,专业点就是 文本移动的距离 + 浏览 ...
- 学生管理系统增删查基本操作(dom4j/sax技术)
基本代码: student.xml <?xml version="1.0" encoding="UTF-8"?><student> &l ...
- 原生js实现简单轮播的淡入淡出效果
实现这种淡入淡出的轮播关键在于css的一种设置 首先它不能像传统的轮播显示隐藏 或者左右浮动 他应该让其固定定位使其重叠在一起 达到这种效果 然后设置c3动画属性 transition:1s; ...
- 用matplotlib库画图
1.用例一 import matplotlib.pyplot as plt import numpy as np x=np.linspace(0,10,100) y=np.cos(2*np.pi*x) ...
- 变分自编码器(Variational auto-encoder,VAE)
参考: https://www.cnblogs.com/huangshiyu13/p/6209016.html https://zhuanlan.zhihu.com/p/25401928 https: ...
- 数据结构之栈和队列及其Java实现
栈和队列是数据结构中非常常见和基础的线性表,在某些场合栈和队列使用很多,因此本篇主要介绍栈和队列,并用Java实现基本的栈和队列,同时用栈和队列相互实现. 栈:栈是一种基于“后进先出”策略的线性表.在 ...
- C# 程序关闭托盘图标不会自动消失
c#程序关闭托盘图标不会自动消失,进程的托盘图标却不能随着进程的结束而自动消失 必须将鼠标移到图标上面时才能消失? 请问如何才能做到图标随着进程的结束而自动消失呢(外部强行结束,如在任务管理器将其 ...
- 【转】Odoo装饰器: one装饰
one装饰器的作用是对每一条记录都执行对应的方法,相当于traditional-style中的function,无返回值! 应用举例: 定义的columns now = fields.Datetime ...
- 佛山Uber优步司机奖励政策(7月27日-8月2日)
周上线时间少于等于7个小时,以下奖励条件无效,仅返还车资. 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册Uber司机(全国版最新最详细注册流程)/月入2万/ ...