问题:

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. MySQL连表操作之一对多

    引入 当我们在数据库中创建表的时候,有可能某些列中值内容量很大,而且重复. 例子:创建一个学生表,按学校年纪班级分,表的内容大致如下: id name partment 1 xxx x学校x年级x班级 ...

  2. hadoop在网页客户端的maven配置

    hadoop网页客户端maven配置,只能在tomcat7上运行,tomcat6和tomcat8运行会出错,我用的是tomcat-7.0.67 完整的pom.xml内容为: <!-- 这个配置只 ...

  3. 初识Jsoup之解析HTML

    按照国际惯例,我首先应该介绍下Jsoup是个什么东西,然后在介绍下具体用法,然后在来个demo演示,其实我也是这么想的,小编今天花了一天的时间从学习—>解析页面,总算是成果圆满了吧,啊哈,但是, ...

  4. IP地址,子网掩码、默认网关,DNS服务器是什么意思?

    (一)  问题解析001.   问:  IP地址,子网掩码,默认网关,DNS服务器,有什么区别呀?我知道没有IP地址就不能上网,我也知道没设DNS就不能上外网,可它们都有什么功能,有什么区别呢?还有真 ...

  5. easyUi datagrid 返回时间格式化操作

    1.格式化返回的时间 { field : 'endTime', title : '轮播图片循环的结束时间', width : 50, align : 'center' //格式化时间操作 format ...

  6. [Unity3D]脚本生命周期学习

    脚本的生命周期 继承于MonoBehaviour的类对象无需手动实例化,由引擎来决定实例化的时机 Awake:每当脚本被加载的时候调用一次,就是说即使脚本没有被勾选,也会调用一次,主要用来做一些初始化 ...

  7. 四、Shell输入、输出功能和字符颜色设置

    一.Shell输入功能 1.键盘输入   方式一: [root@Salve four]# cat test.sh #!/bin/bash #-e 参数可以解析语句中的转义字符 echo -e &quo ...

  8. 安装 Ruby, Rails 运行环境 常见的错误

    安装部署ruby on rails 的环境时并不是想的那么顺利 这个是我遇到的问题及解决的方式 参考安装博客: (1) https://ruby-china.org/wiki/install_ruby ...

  9. Hadoop里的数据挖掘应用-Mahout——学习笔记<三>

    之前有幸在MOOC学院抽中小象学院hadoop体验课. 这是小象学院hadoop2.X的笔记 由于平时对数据挖掘做的比较多,所以优先看Mahout方向视频. Mahout有很好的扩展性与容错性(基于H ...

  10. Java Web 学习链接

    解决JSP中文乱码问题:http://www.cnblogs.com/chengkai/articles/2171848.html 编程思想之多线程与多进程:http://blog.csdn.net/ ...