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 everynode never differ by more than 1.

[解题思路]

检查每个node是否是balanced,大数据集挂掉了

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
if(root == null){
return true;
} return checkBalance(root);
} public boolean checkBalance(TreeNode node){
if(node == null){
return true;
}
int left = getDepth(node.left);
int right = getDepth(node.right);
if(Math.abs(left - right) > 1){
return false;
}
return checkBalance(node.left) && checkBalance(node.right);
} public int getDepth(TreeNode node){
if(node == null){
return 0;
}
int left = getDepth(node.left);
int right = getDepth(node.right);
return Math.max(left, right) + 1;
}
}

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

  1. LeetCode: Balanced Binary Tree 解题报告

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

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

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

  3. LeetCode——Balanced Binary Tree(判断是否平衡二叉树)

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

  4. LeetCode - Balanced Binary Tree

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

  5. [leetcode]Balanced Binary Tree @ Python

    原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...

  6. [Leetcode] Balanced binary tree平衡二叉树

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

  7. LeetCode Balanced Binary Tree (判断平衡树)

    题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...

  8. LeetCode——Balanced Binary Tree

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

  9. [LeetCode] Balanced Binary Tree 深度搜索

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

随机推荐

  1. Sphinx全文检索引擎测试

    数据表 1.documents CREATE TABLE `documents` ( `id` int(13) NOT NULL auto_increment, `group_id` int(11) ...

  2. 简单hello world

    第一步配置路由: 打开app/http/route.php文件,输入:Route::get('/home', 'HomeController@index'); 第二步配置控制器: 控制文件可以手动添加 ...

  3. C#DirectX播放视频

    文章地址:http://www.cnblogs.com/zhangjun1130/archive/2009/09/15/1566814.html 很多人第一次接触到DirectX大都是通过游戏,至于安 ...

  4. PHP部分常见算法

    撰于:http://blog.csdn.net/caleng/article/details/5276403

  5. java中native关键字的用法

    前言: 如果阅读过JDK的源码,我们会发现Thread.java类里有一个方法比较特殊 private native void start0(); 概念: native关键字说明其修饰的方法是一个原生 ...

  6. atitit.软件gui按钮and面板---os区-----软链接,快捷方式

    atitit.软件gui按钮and面板---os区-----软链接,快捷方式 1. 硬链接 1 2. 二.软链接(符号链接)LN 1 3. 三.删除链接 2 4. 区别 2 5. 参考 3 1. 硬链 ...

  7. beyond compare比较工具设置

    beyond compare用于比较的工具,云盘:比较   链接: https://pan.baidu.com/s/1boZbB0F

  8. CYQ学习主要摘要2

    数据库配置假设如下: <connectionStrings>         <add name="Conn" connectionString="Se ...

  9. Linksys WRT54G2 V1刷ddwrt注意事项

    关于DD-WRT和TOMATO下的应用就不多说了,反正其他能刷DD-WRT.TOMATO的路由器会有的功能,这台机器也都有,不过此机器刷TOMATO一定要刷ND版本的,因为5354的CPU是属于比较新 ...

  10. django 使用post方法出现403错误的解决办法

    当采用客户端象django的服务器提交post请求时.会得到403,权限异常.因为django针对提交的请教,有校验.所以会如此. 解决办法: 导入模块:from django.views.decor ...