/**
* 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. VM虚拟机安装的XP如何全屏

    首先安装install VMwear Tools..,如图:

  2. Dubbo原理简介、与Zookeeper整合利用

    官方文档:http://dubbo.io/books/dubbo-user-book/ Dubbo的简单介绍 Dubbo是一个分布式服务框架,架构如图: 节点角色说明: Provider: 暴露服务的 ...

  3. bzoj1077

    题解 这道题n的范围很小,所以我们可以考虑枚举+判定 设放在天平右边的是C,D. 以A+B<C+D为例,因为差分约束必须是差的形式,所以我们将式子变形 B−C<D−A然后枚举D,A的取值, ...

  4. HTML5安全:CORS(跨域资源共享)简介【转】

    前言:像CORS对于现代前端这么重要的技术在国内基本上居然很少有人使用和提及,在百度或者Google上搜索CORS,搜到的中文文章基本都是 另外一种卫星定位技术CORS的介绍,让我等前端同学情何以堪( ...

  5. Visual Studio 后期生成事件命令行

    set "str=$(ConfigurationName)" if "%str%"=="Release" (xcopy /y/e $(Tar ...

  6. Hibernate结合JPA编写通用泛型多条件查询

    项目中使用Hibernate和JPA对数据库对象进行实例化,但是生成的方法不支持多条件查询.而如果针对每一个数据库对象进行多条件查询编码,则会变得很麻烦,而且一旦以后发生表结构发生变化,这些方法可能还 ...

  7. 互评Alpha版本

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2323] 队名:二次元梦之队 组长:刘莹莹 组员:周昊 潘世维  王玉潘 赵美增 ...

  8. generator.xml文件与相关配置插件

    一,generator.xml配置信息 1 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  9. spring-security-4 (5)spring security Java配置实现自定义表单认证与授权

    前面三篇讲解了spring security的搭建以及简单的表单认证与授权原理.本篇将实现我们自定义的表单登录与认证.  本篇不会再讲项目的搭建过程,因为跟第二节的搭建如出一辙.本篇也不会将项目中所有 ...

  10. 整理关于Java进行word文档的数据动态数据填充

    首先我们看下,别人整理的关于Java生成doc 的 资料. java生成word的几种方案 1. Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用 ...