题目:

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.

思路:

1) 计算结点的高度,但这个有重复计算

package tree;

public class BalancedBinaryTree {

    public boolean isBalanced(TreeNode root) {
if (root == null) return true;
int leftHeight = height(root.left);
int rightHeight = height(root.right);
return Math.abs(leftHeight - rightHeight) <= 1 &&
isBalanced(root.left) && isBalanced(root.right);
} private int height(TreeNode root) {
if (root == null) return 0;
return Math.max(1 + height(root.left), 1 + height(root.right));
} }

2) 不进行重复计算,但需要new 对象,反而也花时间

package tree;

public class BalancedBinaryTree {

    class Entry{
public int height;
public boolean balanced;
} public boolean isBalanced(TreeNode root) {
return checkTree(root).balanced;
} private Entry checkTree(TreeNode root) {
Entry entry = new Entry();
if (root == null) {
entry.height = 0;
entry.balanced = true;
} else {
Entry left = checkTree(root.left);
Entry right = checkTree(root.right);
entry.height = Math.max(left.height, right.height) + 1;
entry.balanced = Math.abs(left.height - right.height) <= 1 && left.balanced && right.balanced;
}
return entry;
} }

LeetCode - Balanced Binary Tree的更多相关文章

  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 @ Python

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

  5. leetcode -- Balanced Binary Tree TODO

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

  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. javascript 设计模式-----模块模式

    在一些大的项目中经常使用到模块,在这里,我们将了解一下什么是模块模式.模块模式最简单的方法大家一定会用过,如下所示: var a = { b : 1, c : 2 } 这样一个对象的直接量其实就已经是 ...

  2. 阿里巴巴-OS事业群-OS手机事业部-系统服务部门招聘Java开发工程师,有意者请进来

    我是阿里巴巴-OS事业群-OS手机事业部-系统服务部的开发工程师,正在招聘Java开发工程师. 以下是职位描述: 岗位名称:Java开发工程师 招聘人数:5人 生效日期:2014-03-12 结束日期 ...

  3. 谁能完全搞懂Visual Studio的安装项?

    大家都知道,Visual Studio绝对不是“一个程序”那么简单,不管哪个版本,安装好之后总会在“删除程序”中生成一大堆你或懂或不懂的东西.但很少人关注过究竟包括哪些东西.我最近装了一次Visual ...

  4. 用“MEAN”技术栈开发web应用(二)express搭建服务端框架

    上一篇我们讲了如何使用angular搭建起项目的前端框架,前端抽象出一个service层来向后端发送请求,后端则返回相应的json数据.本篇我们来介绍一下,如何在nodejs环境下利用express来 ...

  5. node访问iis使用keep-alive设置不当

    iis默认的连接超时时间为2分钟 ,因此node程序使用keep-alive访问时,keep-alive的时间不应该超过2分钟,否则在请求完成后,node端继续保持连接,2分钟后iis断开连接,会导致 ...

  6. 爱上MVC~一个Action多套View模版的设计

    回到目录 模块化 这个问题是在做模块化设计时出现的,在Lind.DDD.Plugins模块里,需要对应的模块实体,模块管理者,模块标识接口等,开发时,如果你的功能点属于一个模块,需要实现IPlugin ...

  7. 锋利的JQuery —— JQuery性能优化

    大图猛戳

  8. Android开发学习之路-SimpleAdapter源码分析学习

    今天在课堂上,老师用到了SimpleAdapter,然后女神在边上问我为什么这个SimpleAdapter不能做到我app那种带有进度条的效果,言语说不清,然后就开始看源代码,发现这个Adapter的 ...

  9. [C#基础]基础知识一: 面向对象的基本知识.

    激励自己有时间多看看.!! C#基础共分为七个部分: 一: 面向对象 二: 值类型, 引用类型, 字符串操作 三: 集合文件操作 四: 正则表达式 五: XML操作 六: 委托, 事件 七: 反射 1 ...

  10. Piwik延迟加载脚本

    piwik有时候会卡页面,为了不影响页面,可以将Piwik代码放到最后加载 <!-- Piwik --> <script type="text/javascript&quo ...