• 《剑指offer》第五十五题(平衡二叉树)


    // 面试题55(二):平衡二叉树
    // 题目:输入一棵二叉树的根结点,判断该树是不是平衡二叉树。如果某二叉树中
    // 任意结点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。
    
    #include <iostream>
    #include "BinaryTree.h"
    
    // ====================方法1====================
    //迭代的从上到下,判断每个节点是否是平衡树,会导致一个节点的深度重复计算
    
    int TreeDepth(const BinaryTreeNode* pRoot)//检测节点深度
    {
        if (pRoot == nullptr)
            return 0;
    
        int nLeft = TreeDepth(pRoot->m_pLeft);
        int nRight = TreeDepth(pRoot->m_pRight);
    
        return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);
    }
    
    bool IsBalanced_Solution1(const BinaryTreeNode* pRoot)//记录
    {
        if (pRoot == nullptr)
            return true;
    
        int left = TreeDepth(pRoot->m_pLeft);
        int right = TreeDepth(pRoot->m_pRight);
        int diff = left - right;
        if (diff > 1 || diff < -1)
            return false;
    
        return IsBalanced_Solution1(pRoot->m_pLeft)
            && IsBalanced_Solution1(pRoot->m_pRight);
    }
    
    // ====================方法2====================
    //从下到上检测,如果节点是平衡树,就记录其深度,每个节点被计算一次
    bool IsBalanced(const BinaryTreeNode* pRoot, int* pDepth);
    
    bool IsBalanced_Solution2(const BinaryTreeNode* pRoot)
    {
        int depth = 0;
        return IsBalanced(pRoot, &depth);
    }
    
    bool IsBalanced(const BinaryTreeNode* pRoot, int* pDepth)
    {
        if (pRoot == nullptr)
        {
            *pDepth = 0;
            return true;
        }
    
        int left, right;
        if (IsBalanced(pRoot->m_pLeft, &left)
            && IsBalanced(pRoot->m_pRight, &right))//if条件是找到子节点开始从下向上判断节点是不是平衡树
        {
            int diff = left - right;
            if (diff <= 1 && diff >= -1)//如果是,记录最大深度
            {
                *pDepth = 1 + (left > right ? left : right);
                return true;
            }
        }
    
        return false;
    }
    
    // ====================测试代码====================
    void Test(const char* testName, const BinaryTreeNode* pRoot, bool expected)
    {
        if (testName != nullptr)
            printf("%s begins:
    ", testName);
    
        printf("Solution1 begins: ");
        if (IsBalanced_Solution1(pRoot) == expected)
            printf("Passed.
    ");
        else
            printf("Failed.
    ");
    
        printf("Solution2 begins: ");
        if (IsBalanced_Solution2(pRoot) == expected)
            printf("Passed.
    ");
        else
            printf("Failed.
    ");
    }
    
    // 完全二叉树
    //             1
    //         /      
    //        2        3
    //       /       / 
    //      4  5     6   7
    void Test1()
    {
        BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
        BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
        BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
        BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
        BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
        BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
        BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
    
        ConnectTreeNodes(pNode1, pNode2, pNode3);
        ConnectTreeNodes(pNode2, pNode4, pNode5);
        ConnectTreeNodes(pNode3, pNode6, pNode7);
    
        Test("Test1", pNode1, true);
    
        DestroyTree(pNode1);
    }
    
    // 不是完全二叉树,但是平衡二叉树
    //             1
    //         /      
    //        2        3
    //       /         
    //      4  5         6
    //        /
    //       7
    void Test2()
    {
        BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
        BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
        BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
        BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
        BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
        BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
        BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
    
        ConnectTreeNodes(pNode1, pNode2, pNode3);
        ConnectTreeNodes(pNode2, pNode4, pNode5);
        ConnectTreeNodes(pNode3, nullptr, pNode6);
        ConnectTreeNodes(pNode5, pNode7, nullptr);
    
        Test("Test2", pNode1, true);
    
        DestroyTree(pNode1);
    }
    
    // 不是平衡二叉树
    //             1
    //         /      
    //        2        3
    //       /         
    //      4  5        
    //        /
    //       6
    void Test3()
    {
        BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
        BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
        BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
        BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
        BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
        BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    
        ConnectTreeNodes(pNode1, pNode2, pNode3);
        ConnectTreeNodes(pNode2, pNode4, pNode5);
        ConnectTreeNodes(pNode5, pNode6, nullptr);
    
        Test("Test3", pNode1, false);
    
        DestroyTree(pNode1);
    }
    
    
    //               1
    //              /
    //             2
    //            /
    //           3
    //          /
    //         4
    //        /
    //       5
    void Test4()
    {
        BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
        BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
        BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
        BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
        BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    
        ConnectTreeNodes(pNode1, pNode2, nullptr);
        ConnectTreeNodes(pNode2, pNode3, nullptr);
        ConnectTreeNodes(pNode3, pNode4, nullptr);
        ConnectTreeNodes(pNode4, pNode5, nullptr);
    
        Test("Test4", pNode1, false);
    
        DestroyTree(pNode1);
    }
    
    // 1
    //  
    //   2
    //    
    //     3
    //      
    //       4
    //        
    //         5
    void Test5()
    {
        BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
        BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
        BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
        BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
        BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    
        ConnectTreeNodes(pNode1, nullptr, pNode2);
        ConnectTreeNodes(pNode2, nullptr, pNode3);
        ConnectTreeNodes(pNode3, nullptr, pNode4);
        ConnectTreeNodes(pNode4, nullptr, pNode5);
    
        Test("Test5", pNode1, false);
    
        DestroyTree(pNode1);
    }
    
    // 树中只有1个结点
    void Test6()
    {
        BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
        Test("Test6", pNode1, true);
    
        DestroyTree(pNode1);
    }
    
    // 树中没有结点
    void Test7()
    {
        Test("Test7", nullptr, true);
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
        Test6();
        Test7();
        system("pause");
        return 0;
    }
  • 相关阅读:
    Sqlite Administrator
    在资源管理器/我的电脑详细信息视图里按下Ctrl+(小键盘+)
    Asp.net 2.0 Membership Provider for db4o 6.1
    测试使用Zoundry发布blog
    我的WCF之旅(8):WCF中的Session和Instancing Management
    我的WCF之旅(7):面向服务架构(SOA)和面向对象编程(OOP)的结合——如何实现Service Contract的继承
    我的WCF之旅(5):面向服务架构(SOA)和面向对象编程(OOP)的结合——如何实现Service Contract的重载(Overloading)
    我的WCF之旅(4):WCF中的序列化[上篇]
    我的WCF之旅(6):在Winform Application中调用Duplex Service出现TimeoutException的原因和解决方案
    我的WCF之旅(4):WCF中的序列化[下篇]
  • 原文地址:https://www.cnblogs.com/CJT-blog/p/10541084.html
Copyright © 2020-2023  润新知