[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 ...
随机推荐
- Xcode 怎么查看代码总行数
打开终端 输入 cd 空格 你的工程路径这里有一个小技巧,你把要统计的文件夹直接拖住拖到终端里,路径就出来了.然后输入find . -name "*.m" -or -name &q ...
- 小程序OSS图片上传
图片上传加水印问题,代码如下! chooseImage: function (e) { var that = this; wx.chooseImage({ sizeType: ['original', ...
- Magazine Ad CodeForces - 803D(二分 + 贪心,第一次写博客)
Magazine Ad The main city magazine offers its readers an opportunity to publish their ads. The forma ...
- shell脚本实现目录的“5S”作业
shell,又称为命令解释器.首先它是一个软件,有很多个版本,现在最流行的为bash,它作为用户和内核沟通的中间桥梁,在系统中起着举足轻重的作用 shell脚本,是一个以.sh结尾的文件,里面是诸如l ...
- vue 整体引入 mint-ui 样式失败
当引入Mint-ui 整体css 时 如果出现了这样的错误, 是指找不到对应的Mint-UI 的css :需要从node_modules里寻找 解决方法是在webpack.config.js(有的项目 ...
- php无限级分类----封装函数
public function catetree($cateRes){//传递过来的数据资源 return $this->sort($cateRes); 调用函数 } public functi ...
- Fax Helper
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk.Que ...
- 【blockly教程】第四章 Blockly之选择结构
今天,我们通过一个游戏来学习选择结构,游戏的地址如下:https://blockly-games.appspot.com/bird?lang=en本游戏分为10关:主要游戏规则如下:①主界面是游戏的运 ...
- JavaScript基础part1
JavaScript介绍 你不知道它是什么就学?这就是一个网页嵌入式脚本语言...仅此而已 JavaScript组成 一个完整的 JavaScript 实现是由以下 3 个不同部分组成的: 核心(EC ...
- springBoot 自定义redisTemplate
package com.atirm.mybatismutiplesource.config.RedisConfig; import com.atirm.mybatismutiplesource.ent ...