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. AIX查看内存卡槽

    1.lscfg -vp|grep Processor 2.lscfg -vp|grep -p Memory

  2. DotNetBar v12.6.0.4 Fully Cracked

    更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.6.0.4 如果遇到破解问题可以与我 ...

  3. Word2013可以写博客

    步骤如下http://www.cnblogs.com/guyichang/p/4629211.html

  4. 使用siege进行Web压力测试

    因为最近公司线上node项目出一些不稳定的情况,考虑在这方面能不能做进一步的优化,既然要做优化首先要知道问题出在哪里? 因为暂无法定位是node层问题还是api层出现问题,由于在开发环境小并发量程序运 ...

  5. switch… case 语句的用法(二)

    总结来说:switch的用法是判断case后面的表达式和switch后面的表达式是否相匹配,一旦case匹配,就会顺序执行后面的程序代码,而不管后面的case是否匹配,直到遇见break.都不匹配找d ...

  6. idea快捷键(转)

    Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*...*/ )Ctrl+D 复制行Ctrl+X 删除行快速修复 alt+enter (modify/cast)代码提示 alt+/ctr ...

  7. numpy中的broadcast

    关于broadcast,官方文档描述如下: Each universal function takes array inputs and produces array outputs by perfo ...

  8. Configuration for Python to run on Android

    It's exiting to learn to make app on Android with python. But when I check <Head First Python> ...

  9. Windows桌面快捷方式图标全部变成同一个图标的解决方法

    今天来个客人,说是电脑的所有程序打开都变成 Adobe Reader 了,打开看了下,刚开始是以为EXE文件关联被修改了,用注册表修复工具弄了下,重启电脑,还是老样子.仔细看了下,原来只是快捷方式变成 ...

  10. iptables 用法

    又有人攻击服务器了,没有办法又的去防,这里简单介绍一种限制指定IP访问的办法.  单个IP的命令是 iptables -I INPUT -s 59.151.119.180 -j DROP 封IP段的命 ...