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

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

解题思路:

递归即可,JAVA实现如下:

    public boolean isBalanced(TreeNode root) {
if(root==null)
return true;
if(Math.abs(maxDepth(root.left)-maxDepth(root.right))>1)
return false;
return isBalanced(root.left)&&isBalanced(root.right);
}
static public int maxDepth(TreeNode root) {
if(root==null)
return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
}

Java for LeetCode 110 Balanced Binary Tree的更多相关文章

  1. Java实现LeetCode 110. Balanced Binary Tree

    /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...

  2. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  3. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  4. leetcode 110 Balanced Binary Tree ----- java

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

  5. Java [Leetcode 110]Balanced Binary Tree

    题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...

  6. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

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

  7. LeetCode 110. Balanced Binary Tree (平衡二叉树)

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

  8. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  9. Leetcode 110. Balanced Binary Tree

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

随机推荐

  1. SVN的Status字段含义

    执行SVN up和svn merge等命令出现在首位置的各字母含义如下: “ ” 无修改 “A” 新增 “C” 冲突 “D” 删除 “G” 合并 “I” 忽略 “M” 改变 “R” 替换 “X” 未纳 ...

  2. Enter Query Mode Search Tricks Using Enter_Query Built-in in Oracle Forms

    In this post you will learn how to specify any condition in enter query mode of Oracle Forms. Whenev ...

  3. Unity -- 材质-Material和预设体-Prefabs

    材质(Materials)用来把网格(Mesh)或粒子渲染器(Particle Renderers)贴到游戏对象上.他们在定义对象怎么被显示发挥重要组成部分.材质包括用于呈现网状或颗粒着色器的参考,所 ...

  4. hdu1595 find the longest of the shortest(Dijkstra)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1595 find the longest of the shortest Time Limit: 100 ...

  5. HttpWebRequest用法实例

    [HttpPost] public ActionResult Setmobile() { string text = "<?xml version='1.0' encoding='UT ...

  6. C语言重要概念汇总

    作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...

  7. ARM内核和架构

    转:深入浅谈,CPU设计原理          CPU的内部架构和工作原理 推荐一本书:编码的奥秘 一.ARM内核和架构 ARM产品越来越丰富,命名也越来越多.很多朋友提问: ARM内核和架构都是什么 ...

  8. Android UI开源框架

    1.Side-Menu.Android 分类側滑菜单,Yalantis 出品. 项目地址:https://github.com/Yalantis/Side-Menu.Android 2.Context ...

  9. hdu1081 最大子矩阵

    最大子矩阵自然直在最大连续子序列的升级版  只是其原理都是用到了动态规划思想     仅仅是矩阵用到了枚举 +合并       把非常多列看成是一列的和 #include<stdio.h> ...

  10. os如何处理键盘的所有按键,显示or不显示,显示是如何显示

    [0]README 0.1) source code and text decription are from orange's implemention of a os , and for comp ...