【本文链接】

http://www.cnblogs.com/hellogiser/p/is-balanced-tree.html

题目】

输入一棵二叉树的根结点,判断该树是不是平衡二叉树。如果某二叉树中任意结点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。例如下图中的二叉树就是一棵平衡二叉树:

分析

之前的博文27.二元树的深度[BinaryTreeDepth]中介绍过如何求二叉树的深度。有了经验之后再解决这个问题,我们很容易就能想到思路。

【方案1】

先判断左右子树是不是平衡的,若平衡再求出左右子树的深度,若深度之差大于1,则不平衡。因为在遍历每个结点时都要求其左右子树的深度,因此时间复杂度是O(n^2)的

【代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 
#include "stdafx.h"
#include <cmath>
#include <algorithm>

/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/5/25
*/

// binary tree node struct
struct BinaryTreeNode
{
    int value;
    BinaryTreeNode *left;
    BinaryTreeNode *right;
};

// Get depth of a binary tree
int TreeDepth(BinaryTreeNode *root)
{
    // the depth of a empty tree is 0
    if(NULL == root)
        ;

// the depth of left sub-tree
    int nLeft = TreeDepth(root->left);
    // the depth of right sub-tree
    int nRight = TreeDepth(root->right);

// depth is the binary tree
);
    // return max(nLeft,nRight)+1;
}

// is balanced tree in O(n^2)
bool IsBalanced(BinaryTreeNode *root)
{
    if(NULL == root)
        return true;
    if(!IsBalanced(root->left))
        return false;
    if(!IsBalanced(root->right))
        return false;
    int leftDepth = TreeDepth(root->left);
    int rightDepth = TreeDepth(root->right);
    )
        return false;
    else
        return true;
}

【方2

在判断左右子树是否平衡的过程中把深度计算出来,这样在对父结点进行平衡判断时就可以不用再重复计算左右子树的深度了。其时间复杂度为O(n)

【代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
// is balanced tree in O(n)
bool IsBalanced(BinaryTreeNode *root, int &depth)
{
    if(NULL == root)
    {
        depth = ;
        return true;
    }

int leftDepth, rightDepth;
    if(!IsBalanced(root->left, leftDepth))
        return false;
    if(!IsBalanced(root->right, rightDepth))
        return false;

// get root depth without visiting left and right sub-trees
);
    )
        return false;
    else
        return true;
}

// is balanced tree
bool IsBalancedTree(BinaryTreeNode *root)
{
    int depth;
    return IsBalanced(root, depth);
}

【参考】

http://zhedahht.blog.163.com/blog/static/25411174201142733927831/

http://blog.csdn.net/zjull/article/details/11646591

http://blog.csdn.net/luckyxiaoqiang/article/details/7518888

【本文链接】

http://www.cnblogs.com/hellogiser/p/is-balanced-tree.html

56. 2种方法判断二叉树是不是平衡二叉树[is balanced tree]的更多相关文章

  1. 【java基础 13】两种方法判断hashmap中是否形成环形链表

    导读:额,我介绍的这两种方法,有点蠢啊,小打小闹的那种,后来我查了查资料,别人都起了好高大上的名字,不过,本篇博客,我还是用何下下的风格来写.两种方法,一种是丢手绢法,另外一种,是迷路法. 这两种方法 ...

  2. 3种方法判断手机浏览器跳转WAP手机网站

    随着移动设备的普及,企业的网络宣传已经不能局限在PC端,而需要同时在移动端有所建树.对于公司网站来说,以前都是做的PC端的,当然手机等移动端也可以访问,但是用户体验肯定不如完全适合的手机端来的方便.我 ...

  3. C# winform三种方法判断文本框textBox内容是否为空

    使用系统API函数,需要使用命名空间:System.Runtime.InteropServices: 1.if (textBoxPath.Text ==  String.Empty ) 2.if (t ...

  4. 【遍历二叉树】10判断二叉树是否平衡【Balanced Binary Tree】

    平衡的二叉树的定义都是递归的定义,所以,用递归来解决问题,还是挺容易的额. 本质上是递归的遍历二叉树. ++++++++++++++++++++++++++++++++++++++++++++++++ ...

  5. js中判断数据类型的四种方法总结

    js中判断数据类型的四种方法 前言 在js中,我们经常需要判断数据的类型,那么哪些方法可以用来判断数据的类型呢?哪种方法判断数据类型最准确呢? 我们来一个个分析: 1.typeof typeof是一个 ...

  6. 【剑指offer】判断二叉树是否为平衡二叉树

    2013-09-03 14:16:51 面试题39:求二叉树的深度.判断二叉树是否为平衡二叉树 小结: 根据平衡二叉树的定义,需要判断每个结点,因此,需要遍历二叉树的所有结点,并判断以当前结点为根的树 ...

  7. JS 判断数据类型的三种方法

    说到数据类型,我们先理一下JavaScript中常见的几种数据类型: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Functi ...

  8. 使用c#检测文件正在被那个进程占用 判断文件是否被占用的两种方法

    C# 判断文件是否被占用的三种方法 using System.IO; using System.Runtime.InteropServices; [DllImport("kernel32.d ...

  9. C#判断字符串为空的几种方法和效率判断

    C#判断字符串为空的几种方法和效率判断 string定义 1.1 string str1="":会定义指针(栈),并在内存里划一块值为空的存储空间(堆),指针指向这个空间.1.2 ...

随机推荐

  1. 模式匹配KMP算法

    关于KMP算法的原理网上有很详细的解释,我试着总结理解一下: KMP算法是什么 以这张图片为例子 匹配到j=5时失效了,BF算法里我们会使i=1,j=0,再看s的第i位开始能不能匹配,而KMP算法接下 ...

  2. 查询条件Where

    1.字符串 $condition = 'name=\'Lily\' and age>10'; 2.数组 ['type' => 1, 'status' => 1] //生成 (type ...

  3. JDK,JRE,JVM区别与联系(ZZ)

    http://www.cnblogs.com/hencehong/p/3252166.html 我们开发的实际情况是:我们利用JDK(调用JAVA API)开发了属于我们自己的JAVA程序后,通过JD ...

  4. tomcat7禁用catalina.out输出

    tomcat7中禁用catalina.out的输出,又可能很大. 直接修改catalina.sh文件的输出语句. 在文件中找到以下内容. if [ -z "$CATALINA_OUT&quo ...

  5. shared_ptr<> reset

    // std_tr1__memory__shared_ptr_reset.cpp // compile with: /EHsc #include <memory> #include < ...

  6. subplot的应用

    import matplotlib.pyplot as Plot Plot.subplot(3, 4, (1, 7)) Plot.subplot(1, 4, 4) Plot.subplot(3, 4, ...

  7. Get value from agent failed: cannot connect to [[192.168.186.130]:10050]: [113]No route to host

    客户端配置zabbix-agent 后,网页端出现Get value from agent failed: cannot connect to [[192.168.186.130]:10050]: [ ...

  8. Ubuntu 为网卡配置静态IP地址

    为网卡配置静态IP地址编辑文件/etc/network/interfaces:sudo vi /etc/network/interfaces并用下面的行来替换有关eth0的行:# The primar ...

  9. Ubuntu 下忘记mysql 密码

    在Ubuntu下忘记密码.可按以下步骤重设:1.停止mysql服务: #/etc/init.d/mysql stop;2,安全模式启动mysql,略过密码验证: # sudo mysqld_safe ...

  10. ExtJS学习之路第四步:看源码,实战MessageBox

    可以通过看MessageBox.js的源码来深入认识,记住它的主要用法.Ext.MessageBox是实用类,用于生成不同风格的消息框,它是Singleton(单例),别名Ext.Msg.注意Mess ...