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. vue 项目 切换手机端和pc端。同一个项目,配置不同的路由

    1, 首先判断设备:在main.js里面写 // vue原型挂载 - 是否PC端 if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator. ...

  2. Git简单配置ssh秘钥

    执行以下命令: git config --global user.name "demo" git config --global user.email "demo@dem ...

  3. laydate5.0 设置最大最小值

    由于新版的laydate时间插件在初始化时已设置时间最大最小范围,且生成对象,无法重新渲染改变其日期最大最小值. 有网友经实验贴出如下方法可达成目的,故做记录. //开始时间 var startDat ...

  4. requests+mongodb爬取今日头条,多进程

    import json import os from urllib.parse import urlencode import pymongo import requests from bs4 imp ...

  5. [HDU6321]Dynamic Graph Matching(DP)

    题意:给定一个n个点的无向图,开始没有边,然后m个操作,每次加边或者删边,每次操作后输出正好k个边的匹配数k=1,2,3,...n/2,n<=10,m<=30000 可以发现,n<= ...

  6. Node.js的Formidable模块的使用,方便快捷

    服务用的是express ,如果不是很老的express框架,都有自带formidable  如果没有就下载一个  npm i formidable var formidable = require( ...

  7. tomcat8.5配置高并发

    最近部署的tomcat应用,有一天压测的时候,测试一致反馈下载不了,结果查看日志才发现如下错误: INFO: Maximum number of threads (200) created for c ...

  8. 武汉Uber优步司机奖励政策(12月14日到12月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. 5 多进程copy文件

    1.如何进行开发? 2.版本1:程序大框架 #1.创建一个文件夹 #2.获取old文件夹中所有的文件名字 #3.使用多进程的方式copy原文件夹中的所有文件到新文件夹中 3.版本2:创建一个文件夹 1 ...

  10. springboot jpa操作redis

    SpringBoot使用Redis缓存   (1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boo ...