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,大数据集挂掉了

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public boolean isBalanced(TreeNode root) {
12         // Start typing your Java solution below
13         // DO NOT write main() function
14         if(root == null){
15             return true;
16         }
17
18         return checkBalance(root);
19     }
20
21     public boolean checkBalance(TreeNode node){
22         if(node == null){
23             return true;
24         }
25         int left = getDepth(node.left);
26         int right = getDepth(node.right);
27         if(Math.abs(left - right) > 1){
28             return false;
29         }
30         return checkBalance(node.left) && checkBalance(node.right);
31     }
32
33     public int getDepth(TreeNode node){
34         if(node == null){
35             return 0;
36         }
37         int left = getDepth(node.left);
38         int right = getDepth(node.right);
39         return Math.max(left, right) + 1;
40     }
41 }

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 ...

  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

    后序遍历,每个节点只遍历一次. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode ...

  4. Leetcode: Balanced Binary Tree

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

  5. LeetCode:Balanced Binary Tree

    题目链接 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  6. [LeetCode]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 平衡二叉树

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

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

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

  9. LeetCode 110. Balanced Binary Tree

    LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...

最新文章

  1. 报错You may use special comments to disable some warnings.vue-cli脚手架关闭eslint的步骤
  2. 库克踏春而来,小而美的 iPhone 全新配件问世
  3. UVA11021麻球繁衍
  4. node.js require 自动执行脚本 并生成html,利用node.js实现自动生成前端项目组件的方法详解...
  5. MySQL学习笔记06【多表查询、子查询、多表查询练习】
  6. static的用法及其与auto的区别小结
  7. 大话深度学习:B站Up主麦叔教你零代码实现图像分类神经网络
  8. sybase SET CHAINED OFF与SET CHAINED ON两种事务模式的区别
  9. linux模拟dnf,每天学一个 Linux 命令:dnf
  10. Linux命令之shutdown
  11. SUBSTRING_INDEX
  12. c语言 adt实验报告,Android实验报告
  13. WireShark抓包原理解析及抓包实战教程
  14. 用 windows 资源监视器 查看 被占用的文件
  15. js实现数字转化为大写金额——js技能提升
  16. linux用户无法接收邮件,linux 下 搭建邮件邮件服务器(Postfix+Dovecot)(一)-系统账户登陆收发邮件...
  17. 用一生的漫长,等待最初的绿色
  18. 直方图的计算,绘制与分析
  19. revit插件怎么快速自动标高?可以使用有自动标高的revit插件来实现
  20. 【无标题】使用element-ui中的el-date-picker,中间的至字显示不全的原因及解决

热门文章

  1. 亲测可用:Anaconda Windows Error:[Error 2]或者系统找不到指定文件
  2. 【DL4J速成】Deeplearning4j图像分类从模型自定义到测试
  3. Petrozavodsk Winter Camp, Day 8, 2014, Mosaic
  4. C++ 实例化对象 p-printX()
  5. Spring4基础 学习笔记(3) ---- AOP(1) ---- 基于Xml
  6. 2016级算法期末模拟练习赛-A.wuli51和京导的毕业旅行
  7. 通过onActivityResult()先跳转到联系人界面,然后把传回来的手机号显示到应用的EditText上...
  8. 几个主流的Java连接池整理
  9. jQuery学习笔记1--表格展开关系
  10. Sublime Text的使用技巧