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.

判断一棵树是否是平衡二叉树

利用高度的答案,递归求解。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) { if( root == null)
return true; int left = maxDepth(root.left);
int right = maxDepth(root.right); if( left - right > 1 || right - left > 1)
return false;
return isBalanced(root.left)&&isBalanced(root.right); } public int maxDepth(TreeNode root) {
if( root == null )
return 0;
if( root.left == null && root.right == null)
return 1;
if( root.left == null)
return maxDepth(root.right)+1;
if( root.right == null)
return maxDepth(root.left)+1;
return Math.max(maxDepth(root.left),maxDepth(root.right))+1;}
}

leetcode 110 Balanced Binary Tree ----- java的更多相关文章

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

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

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

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

  3. Java for LeetCode 110 Balanced Binary Tree

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

  4. Java [Leetcode 110]Balanced Binary Tree

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

  5. Java实现LeetCode 110. Balanced Binary Tree

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

  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. ios 常用的小框架

    在ios开发中,一些请求 kvc 下拉列表  图片请求等等自己手写代码的话非常麻烦,还容易出现一系列的问题,现在整理了一些常用的一些小框架. 其中MJExtension 和 MJRefresh 这两个 ...

  2. Problem A 栈

    Description   You are given a string consisting of parentheses () and []. A string of this type is s ...

  3. Fragment 切换问题

    public void switchContent(Fragment fragment) { if(mContent != fragment) { mContent = fragment; mFrag ...

  4. static inline

    今天看到了这样一段代码, static inline BOOL IsEmpty(id thing) { return thing == nil || [thing isEqual:[NSNull nu ...

  5. java.lang包的分类

    提供利用 Java 编程语言进行程序设计的基础类. 1>  最重要的类是 Object(它是类层次结构的根)和 Class(它的实例表示正在运行的应用程序中的类).   2>  把基本类型 ...

  6. 2016年3月AV评测

  7. Ubuntu 14.10 下安装SVN

    本文主要介绍SVN独立服务器的的安装和简单配置:1.安装 # sudo apt-get install subversion 测试安装是否成功: # svnserve --version 回车显示版本 ...

  8. hdu 2083

    ps:N个数中,中位数是最小距离...第一次WA是因为排序之后最小和最大相加除2...应该是找他们的中位数,而不是中间数. 代码: #include "stdio.h" #incl ...

  9. java互斥方法

    synchronized,  lock/unlock,  volatile类型变量, atom类, 同步集合,  新类库中的构件: CountDownLatch\CyclicBarric\Semaph ...

  10. M1-S70卡片介绍

    卡片有4K的存储空间,有32个小扇区和8个大扇区.小扇区的结构为:每扇区有4块,每块16个字节,一共64字节,第3块为密钥和控制字节:大扇区的结构为:每扇区16块,每块16个字节,一共256字节,第1 ...