题目:

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. Aoite 系列 目录

    介绍 本项目从2009年孵化(V->Sofire->Aoite),至今已度过5个年头.一直在优化,一直在重构,一直在商用.有十分完整的单元测试用例.可以放心使用. Aoite on 博客园 ...

  2. 初探ECMAScript6

    基础变化 String类型新增了三个方法,不必使用indexOf来判断一个字符串是否在另一个字符串内 //String changes var a = "Hello world"; ...

  3. 上网调查一下目前流行的源程序版本管理软件和项目管理软件都有哪些?各有什么优缺点?并在Github注册账户,写一篇博客记录注册的过程。

    问题一:上网调查一下目前流行的源程序版本管理软件和项目管理软件都有哪些?各有什么优缺点?(以下内容为网上查询所得) Microsoft TFS(Team Foundation Server): 优点: ...

  4. 如何捕获和分析 JavaScript Error

    前端工程师都知道 JavaScript 有基本的异常处理能力.我们可以 throw new Error(),浏览器也会在我们调用 API 出错时抛出异常.但估计绝大多数前端工程师都没考虑过收集这些异常 ...

  5. Android Activity的生命周期简单总结

    Android Activity的生命周期简单总结 这里的内容参考官方的文档,这篇文章的目的不是去总结Activity是如何启动,如何创造,以及暂停和销毁的,而是从实际开发中分析在Activity各个 ...

  6. jQuery疑惑记录

    不断更新 1.项目中有一句 this.$html = $('<div/>').html(html).attr('sspa-mod-id', this.modName).hide();不知道 ...

  7. python面向对象随笔

    传送门:http://www.runoob.com/python/python-object.html

  8. MVVM架构~knockoutjs系列之Mapping插件为对象添加ko属性

    返回目录 对于一个JS对象来说,如果希望将所有属性进行监视,在之前我们需要一个个对属性添加ko.observable方法,而有了Mapping插件后,它可以帮助我们这件事. 在Mapping出现之前 ...

  9. paip.提升性能--多核cpu中的java/.net/php/c++编程

    paip.提升性能--多核cpu中的java/.net/php/c++编程 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http ...

  10. iOS-Delegate模式

    代理模式 顾名思义就是委托别人去做事情. IOS中经常会遇到的两种情况:在cocoa框架中的Delegate模式与自定义的委托模式.下面分别举例说明一下: 一.cocoa框架中的delegate模式 ...