/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int height(TreeNode node)
{
if (node == null)
{
return ;
}
int lH = height(node.left);
if (lH == -)
{
return -;
}
int rH = height(node.right);
if (rH == -)
{
return -;
}
if (lH - rH < - || lH - rH > )
{
return -;
}
return Math.Max(lH, rH) + ;
} public bool IsBalanced(TreeNode root)
{
if (root == null)
{
return true;
}
return height(root) != -; }
}

https://leetcode.com/problems/balanced-binary-tree/#/description

补充一个python的实现:

 class Solution:
def __init__(self):
self.result = True def maxDepth(self,root):
if root == None:
return
l = self.maxDepth(root.left)
r = self.maxDepth(root.right)
d = max(l,r) +
if abs(l - r) > :
self.result = False
return d def isBalanced(self, root: TreeNode) -> bool:
self.maxDepth(root)
return self.result

leetcode110的更多相关文章

  1. leetcode-110:判断平衡二叉树 Java

    Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...

  2. [Swift]LeetCode110. 平衡二叉树 | Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  3. LeetCode110.平衡二叉树

    一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,null,15,7] 3 / \ 9 20 / \ 15 7 返回 true . 示例 ...

  4. LeetCode110 Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  5. leetcode110:combination-sum-ii

    题目描述 给出一组候选数C和一个目标数T,找出候选数中起来和等于T的所有组合. C中的每个数字在一个组合中只能使用一次. 注意: 题目中所有的数字(包括目标数T)都是正整数 组合中的数字 (a 1,  ...

  6. leetcode_二叉树篇_python

    主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...

  7. LeetCode通关:连刷三十九道二叉树,刷疯了!

    分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...

随机推荐

  1. Xmind8 (update8)破解教程

    环境:Win7 一.下载xmind: Xmind版本:xmind-8-update8-windows.exe  百度云盘地址: https://pan.baidu.com/s/1ccd18E1hOY1 ...

  2. bzoj2759

    题解: lct+解线性方程组 首先先把每一个环搞出来,然后再建立一个额外的点 然后解方程.. 代码: #include <bits/stdc++.h> using namespace st ...

  3. Java Web编程的主要组件技术——Hibernate核心组件

    参考书籍:<J2EE开源编程精要15讲> Hibernate配置文件 1) hibernate.cfg.xml <?xml version='1.0' encoding='UTF-8 ...

  4. ISE创建Microblaze软核(一)

    在使用FPGA时,有时会用到它做为主控芯片.对于习惯于单片机及C语言开发的人,使用FPGA做主控芯片,首先还是想到它的嵌入式软核功能.如果能够基于Microblze软核进行C语言程序的开发,相对于使用 ...

  5. Jenkins无法读取覆盖率报告的解决方法

    报错信息如下: log 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 coverage-report: [mkdir] Cre ...

  6. 在jenkins和sonar中集成jacoco(二)--在jenkins中生成jacoco覆盖率报告

    先要在jenkins上安装jacoco的插件,安装完成之后在job的配置项中可以增加这个选项: 第一个录入框是你的覆盖率文件(exec),第二个是class文件目录,第三个是源代码文件目录. 配置好了 ...

  7. [批处理]NetstatFilter快速查找端口被占用问题

    前言 准确的说,他是一个网络连接端口查看器,可以根据进程查端口,也可以根据端口查进程.期初是因在使用Fiddler的时候发现无法启动,提示端口被占用,但是由不知道用什么方法才能找到是哪个程序占用的Fi ...

  8. 201621123005《Java程序设计》第十三次实验总结

    <Java程序设计>第十三周实验总结 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主 ...

  9. Alpha阶段第1周/共2周 Scrum立会报告+燃尽图 03

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2248 一.小组介绍 组长:刘莹莹 组员:朱珅莹 孙韦男 祝玮琦 王玉潘 ...

  10. Winform开发常用控件之DataGridView的简单数据绑定——代码绑定DataSet、DataTable、IList、SqlDataReader

    前文介绍了Winform为DataGridView提供的数据自动绑定功能,下面介绍一下采用代码的数据绑定 1.用DataSet和DataTable为DataGridView提供数据源 先上代码 pri ...