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

思路:

递归求子树的和,观察子树的和是否是整个树的和的一半即可。

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]的更多相关文章

  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. 【LeetCode】663. Equal Tree Partition 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  3. 663. Equal Tree Partition 能否把树均分为求和相等的两半

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

  4. [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 ...

  5. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  6. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  7. 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 ...

  8. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  9. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  10. [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 ...

随机推荐

  1. 单源最短路dijkstra算法&&优化史

    一下午都在学最短路dijkstra算法,总算是优化到了我能达到的水平的最快水准,然后列举一下我的优化历史,顺便总结总结 最朴素算法: 邻接矩阵存边+贪心||dp思想,几乎纯暴力,luoguTLE+ML ...

  2. HTML中汉字空格占位符

    == 普通的英文半角空格   ==   ==   == no-break space (普通的英文半角空格但不换行)   == 中文全角空格 (一个中文宽度)   ==   == en空格 (半个中文 ...

  3. foreach传值

  4. shell入门基础&常见命令及用法

    shell shell是一个命令解释器,实际是一个程序,/bin/bash,linux中所有的命令都由它来解释,有自己的语法 shell脚本 以.sh结尾 shell语法+linux命令 注释: 单行 ...

  5. 【NXP开发板应用—智能插排】3.驱动GPIO点亮外接LED

    [前言] 首先感谢深圳市米尔科技有限公司举办的这次活动并予以本人参加这次活动的机会,以往接触过嵌入式,但那都是皮毛,最多刷个系统之类的,可以说对于嵌入式系统开发这件事情是相当非常陌生的,这次活动为我提 ...

  6. buck型DC-DC分析

    BUCK型DC/DC电源分析 这种buck型DC/DC电路的拓扑结构:( 1N5822叫续流二极管!) LM2756相当于高速断开和闭合的开关,连接在Vin与Vout脚之间. 1. 在开关闭合时,电流 ...

  7. python 感叹号的作用

    1.     !表示反转逻辑表达式的值 2.      打印格式控制中: x!r代表repr(x),x!s代表str(x),x!a代表ascii(x)

  8. 03以太网帧结构(链路层 IEEE802.3)

    OSI七层模型:从底往上记(研究细致时用) 物理层:单位bit,字节byte,同轴电缆,光纤,二进制,比特流 数据链路层:帧,16进制,0-9,A-FMac地址->全网唯一性     mac地址 ...

  9. Java基础之instanceof和transient关键字用法

    instanceof 用于检测指定对象是否是某个类(本类.父类.子类.接口)的实例.Java中的instanceof也称为类型比较运算符,因为它将类型与实例进行比较. 返回true或false. 如果 ...

  10. 破解有道词典在线翻译接口--python

    没什么好说的,直接上代码. import requests # import json import time import random import hashlib translate = inp ...