问题:

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,但马上发现这是一个递归过程,每次递归返回的是深度,可是还得判断是否平衡,如果不平衡如何返回是否平衡,

然后你可能会想到使用两个函数,一个函数用于递归求深度,一个函数用于递归求是否平衡,如下:

public boolean isBalanced(TreeNode root) {
if(root==null) return true;
int l=depth(root.left);
int r=depth(root.right);
return ((int)Math.abs(l-r)<2)&&isBalanced(root.left) && isBalanced(root.right);
}
static int depth(TreeNode n){
if(n==null) return 0;
return Math.max(depth(n.left),depth(n.right))+1;
}

再然后你会发现时间复杂度为O(n^2),做了很多的无用功。

 

要想降低时间复杂度,就得想一个办法让我们在递归求深度的同时判断是否是平衡二叉树,也就是还是得解决求深度的时候递归返回值的问题,在LeetCode中discuss了一下,然后发现了大神们用了一个求深度时不可能出现的值轻松解决问题,关键代码如下:

public final int UNB = -99;
public int balanceJudge(TreeNode root){
if(root==null)return 0;
int l = balanceJudge(root.left);
int r = balanceJudge(root.right);
if(l==UNB || r== UNB || Math.abs(l-r)>1) return UNB;
return 1+(l>r?l:r);
}

最后只需要判定返回值否为UNB就可以知道改二叉树是否平衡了。。

 

 


完整代码如下(java):

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public final int UNB = -99;
public boolean isBalanced(TreeNode root) {
int result = balanceJudge(root);
if(result != UNB)return true;
else return false;
} public int balanceJudge(TreeNode root){
if(root==null)return 0;
int l = balanceJudge(root.left);
int r = balanceJudge(root.right);
if(l==UNB || r== UNB || Math.abs(l-r)>1) return UNB;
return 1+(l>r?l:r);
}
}

LeetCode——Balanced Binary Tree(判断是否平衡二叉树)的更多相关文章

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

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

  2. LeetCode: Balanced Binary Tree 解题报告

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

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

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

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

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

  5. balanced binary tree(判断是否是平衡二叉树)

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

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

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

  8. LeetCode - Balanced Binary Tree

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

  9. 【easy】110. Balanced Binary Tree判断二叉树是否平衡

    判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...

随机推荐

  1. 快速上手php:使用PhpStrom部署项目

    闲话 上学的时候一直以为可以专注自己的领域,以为与php无缘的,但是这种想法是错误,在完成任务的时候,你不得不使用你不熟悉的语言或者框架.正所谓业务驱动开发,这次接手已经离职的前辈的留下来的项目,最蛋 ...

  2. 为开发者准备的 Android 函数库(2016 年版)

    转载:http://www.androidchina.net/5922.html第三方函数库(译者注:包括第三方提供的 SDK,开源函数库)以惊人的方式助力着 Android 开发,借助这些其他开发人 ...

  3. wpf *和auto的区别

    Auto 表示自动适应显示内容的宽度, 如自动适应文本的宽度,文本有多长,控件就显示多长. * 则表示按比例来分配宽度. <ColumnDefinition Width="3*&quo ...

  4. BZOJ1598: [Usaco2008 Mar]牛跑步

    传送门 K短路,普遍的算法是采用AStar求解,先建立反向边跑一遍dij,或者spfa什么的.跑出反向边的距离就可以看为估价函数中的$h()$.设$dist$为当前已经走过的距离,那么$f(node) ...

  5. Hibernate的关联映射关系

    一:多对一 <many-to-one 1.name:当前类的属性名(关联映射的类) 2.column:属性多对应的类的对应的表的外键(连接条件) 3.class:属性所对应的类的权限定名 4.n ...

  6. ruby

    :for 是关键字, each是方法. for 后面的变量,是全局变量,不仅仅存在于for .. end 这个作用域之内 module中的 self.xx方法可以被直接调用 module中的普通方法, ...

  7. 介绍对称加密算法,最常用的莫过于DES数据加密算法

    DES DES-Data Encryption Standard,即数据加密算法.是IBM公司于1975年研究成功并公开发表的.DES算法的入口参数有三个:Key.Data.Mode.其中Key为8个 ...

  8. 同一网站中HTML相对路径引用

    ../表示目录的上一级 如:一个网站文件夹text里有HTML,JS,CSS....文件夹,HTML文件夹有个text.html, JS文件夹有个text.js, CSS文件夹中有个text.css. ...

  9. Angular.js实现折叠按钮的经典指令.

    var expanderModule=angular.module('expanderModule',[]) expanderModule.directive('expander',function( ...

  10. 10月24日上午PHP面向对象

    面向对象 程序分为两种,一种是面向过程的,另一种是面向对象的.之前的学的都是面向过程的,按部就班的一步一步的按照顺序往下走. 面向对象: 1.什么叫做对象 一切皆为对象(一个对象由一组属性和有权对这些 ...