【本文链接】

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. Struts tag -s

    1,if/elseif/else标签 <s:set value="19"/> <s:if test="%{#age > 60}"> ...

  2. 什么是POJO?

    本文转载自百度文库,详细出处请参考: http://wenku.baidu.com/view/4a9ad533ee06eff9aef80765.html 我认为写的很准确,很抱歉没有找到作者的名字! ...

  3. 【前端学习】搬进Github

    学习参考 萌码 一.Github简介和基本操作 Github 上操作基本上围绕一个个项目展开.项目就是一个文件夹,在github中成为“仓库”(repository),里面放着所有的项目文件,可以是代 ...

  4. 【uoj150】 NOIP2015—运输计划

    http://uoj.ac/problem/150 (题目链接) 题意 给出一棵树以及m个询问,可以将树上一条边的权值修改为0,求经过这样的修改之后最长的边最短是多少. Solution 老早就听说过 ...

  5. POJ2411 Mondriaan's Dream

    Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, af ...

  6. C#获取本机磁盘信息

    照着书敲的.留作笔记吧. using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...

  7. Latex 笔记

    A source file is made up of text, formulas, and instructions (commands) to $\LaTeX.$ Commands start ...

  8. Linux 线程(进程)数限制分析

    1.问题来源公司线上环境出现MQ不能接受消息的异常,运维和开发人员临时切换另一台服务器的MQ后恢复.同时运维人员反馈在出现问题的服务器上很多基本的命令都不能运行,出现如下错误:2.   初步原因分析和 ...

  9. codeforce626D (概率)

    D. Jerry's Protest time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  10. The file couldn’t be opened because you don’t have permission to view it

    because you dont have permission to view it 解决办法 Project---Build Setting中 修改这一项,变成Default Compiler(A ...