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. C++模板类内友元(友元函数,友元类)声明的三种情况

    根据<C++ Primer>第三版16.4节的叙述,C++类模板友元分为以下几种情况 1.非模板友元类或友元函数. 书上给了一个例子: class Foo{     void bar(); ...

  2. 你觉得你非常了解Javascript?

    (翻译不当之处请谅解) 来源:http://www.ido321.com/914.html 这里有5个小脚本,有助于你真正理解JavaScript核心–闭包和作用域.没有在控制台执行之前,尝试回答每一 ...

  3. MFC总结之CListCtrl用法及技巧(一)

    本文根据本人在项目中的应用,来谈谈CListCtrl的部分用法及技巧.当初学习时,查了很多资料,零零碎碎的作了些记录,现在主要是来做个总结,方便以后查阅.主要包括以下十三点内容:基本操作.获取选中行的 ...

  4. SEH, SAFESEH相关

    SEH, SAFESEH相关 1,触发seh异常让目标程序Read/Write无效地址,如果和栈底相邻的内存只读,尝试覆盖超出栈底 2,如何找到(显示)要覆盖的SEHod语法:dd fs:[0]sof ...

  5. store.js 跨浏览器的localStorage

    store.js 跨浏览器的localStorage 我们总是想要储存一些数据在浏览器端,却对复杂的兼容性头疼,store.js很好的解决了这些问题. store.js ☍ 使用它相当简单: // 储 ...

  6. vue使用axios,进行网络请求

    1.首先自己创建一个组件: https://www.cnblogs.com/fps2tao/p/9559291.html 2.安装:axios(可以npm安装,也可以下载js引入文件) npm ins ...

  7. MySQL主从复制状态及数据一致性监测工具

    转: https://blog.csdn.net/m0_37814112/article/details/80914713

  8. 如何实现php异步处理

    在实际生成环境下,php作为后台的接口服务器已经很常见,php当然具有它能作为后台服务器的优势之处,但是,在处理一些客户端并不关心的结果时,就显出它的弊端了---没有异步执行的机制.就比如我们想做一些 ...

  9. Angularjs $http.post

    $http.post 采用postJSON方式发送数据到后台. 如果不需要发送json格式数据,序列化成&连接的字符串,形如:"a=1&b=2",最终完整的前端解决 ...

  10. spawn-fcgi原理及源代码分析

    spawn-fcgi是一个小程序,作用是管理fast-cgi进程,功能和php-fpm类似,简单小巧,原先是属于lighttpd的一部分.后来因为使用比較广泛.所以就迁移出来作为独立项目了.本文介绍的 ...