zoukankan      html  css  js  c++  java
  • 56. 2种方法判断二叉树是不是平衡二叉树[is balanced tree]

    【本文链接】

    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)
            
    return 0;

        
    // 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 (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);
        
    // 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);
        
    if (abs(leftDepth - rightDepth) > 1)
            
    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 = 
    0;
            
    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
        depth = (leftDepth > rightDepth) ? (leftDepth + 1) : (rightDepth + 1);
        
    if (abs(leftDepth - rightDepth) > 1)
            
    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

    个人学习笔记,欢迎拍砖!---by hellogiser

    Author: hellogiser
    Warning: 本文版权归作者和博客园共有,欢迎转载,但请保留此段声明,且在文章页面明显位置给出原文连接。Thanks!
    Me: 如果觉得本文对你有帮助的话,那么【推荐】给大家吧,希望今后能够为大家带来更好的技术文章!敬请【关注】
  • 相关阅读:
    UIButtonIOS开发
    SharePoint Server 2007 SP1 已发布
    SharePoint 2007 External Binary Storage Component Preview 发布
    WSS 3.0 & MOSS 2007 SDK 1.1
    SharePoint工作流(ASP.NET表单版)教学视频
    Finally...
    Windows SharePoint Services 3.0 "Visual How Tos" 视频系列
    SharePoint 补丁
    在SharePoint Workflow中使用InfoPath Form的几个Tips
    对于Office Open XML文档格式,请发表您的看法
  • 原文地址:https://www.cnblogs.com/hellogiser/p/is-balanced-tree.html
Copyright © 2011-2022 走看看