• 《剑指offer》第三十二题(分行从上到下打印二叉树)


    // 面试题32(二):分行从上到下打印二叉树
    // 题目:从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印,每一层
    // 打印到一行。
    
    #include <cstdio>
    #include "BinaryTree.h"
    #include <queue>
    
    void Print(BinaryTreeNode* pRoot)
    {
        if (pRoot == nullptr)
            return;
    
        std::queue<BinaryTreeNode*> nodes;
        nodes.push(pRoot);
        int nextLevel = 0;//相比上个题,多了两个变量,一个是下层结点个数,初始化为0
        int toBePrinted = 1;//一个是待打印的点,初始化为1
        while (!nodes.empty())
        {
            BinaryTreeNode* pNode = nodes.front();
            printf("%d ", pNode->m_nValue);
    
            if (pNode->m_pLeft != nullptr)
            {
                nodes.push(pNode->m_pLeft);
                ++nextLevel;//每有一个孩子进入队列,下层结点加1
            }
            if (pNode->m_pRight != nullptr)
            {
                nodes.push(pNode->m_pRight);
                ++nextLevel;
            }
    
            nodes.pop();
            --toBePrinted;//每弹出一个节点,待打印节点就减1
            if (toBePrinted == 0)//直到当前层打印完
            {
                printf("
    ");//换行
                toBePrinted = nextLevel;//把记住的下层结点总数给它
                nextLevel = 0;//下层结点要打印的个数置零,从新计数
            }
        }
    }
    
    // ====================测试代码====================
    //            8
    //        6      10
    //       5 7    9  11
    void Test1()
    {
        BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
        BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
        BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
        BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
        BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);
    
        ConnectTreeNodes(pNode8, pNode6, pNode10);
        ConnectTreeNodes(pNode6, pNode5, pNode7);
        ConnectTreeNodes(pNode10, pNode9, pNode11);
    
        printf("====Test1 Begins: ====
    ");
        printf("Expected Result is:
    ");
        printf("8 
    ");
        printf("6 10 
    ");
        printf("5 7 9 11 
    
    ");
    
        printf("Actual Result is: 
    ");
        Print(pNode8);
        printf("
    ");
    
        DestroyTree(pNode8);
    }
    
    //            5
    //          4
    //        3
    //      2
    void Test2()
    {
        BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
        BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
        BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
        BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    
        ConnectTreeNodes(pNode5, pNode4, nullptr);
        ConnectTreeNodes(pNode4, pNode3, nullptr);
        ConnectTreeNodes(pNode3, pNode2, nullptr);
    
        printf("====Test2 Begins: ====
    ");
        printf("Expected Result is:
    ");
        printf("5 
    ");
        printf("4 
    ");
        printf("3 
    ");
        printf("2 
    
    ");
    
        printf("Actual Result is: 
    ");
        Print(pNode5);
        printf("
    ");
    
        DestroyTree(pNode5);
    }
    
    //        5
    //         4
    //          3
    //           2
    void Test3()
    {
        BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
        BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
        BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
        BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    
        ConnectTreeNodes(pNode5, nullptr, pNode4);
        ConnectTreeNodes(pNode4, nullptr, pNode3);
        ConnectTreeNodes(pNode3, nullptr, pNode2);
    
        printf("====Test3 Begins: ====
    ");
        printf("Expected Result is:
    ");
        printf("5 
    ");
        printf("4 
    ");
        printf("3 
    ");
        printf("2 
    
    ");
    
        printf("Actual Result is: 
    ");
        Print(pNode5);
        printf("
    ");
    
        DestroyTree(pNode5);
    }
    
    void Test4()
    {
        BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    
        printf("====Test4 Begins: ====
    ");
        printf("Expected Result is:
    ");
        printf("5 
    
    ");
    
        printf("Actual Result is: 
    ");
        Print(pNode5);
        printf("
    ");
    
        DestroyTree(pNode5);
    }
    
    void Test5()
    {
        printf("====Test5 Begins: ====
    ");
        printf("Expected Result is:
    ");
    
        printf("Actual Result is: 
    ");
        Print(nullptr);
        printf("
    ");
    }
    
    //        100
    //        /
    //       50   
    //         
    //         150
    void Test6()
    {
        BinaryTreeNode* pNode100 = CreateBinaryTreeNode(100);
        BinaryTreeNode* pNode50 = CreateBinaryTreeNode(50);
        BinaryTreeNode* pNode150 = CreateBinaryTreeNode(150);
    
        ConnectTreeNodes(pNode100, pNode50, nullptr);
        ConnectTreeNodes(pNode50, nullptr, pNode150);
    
        printf("====Test6 Begins: ====
    ");
        printf("Expected Result is:
    ");
        printf("100 
    ");
        printf("50 
    ");
        printf("150 
    
    ");
    
        printf("Actual Result is: 
    ");
        Print(pNode100);
        printf("
    ");
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
        Test6();
        system("pause");
        return 0;
    }
  • 相关阅读:
    Java低配版简单的随机点名系统
    是否二叉搜索树
    输出学生成绩
    求给定精度的简单交错序列部分和
    字符串的连接
    学生信息链表,建立,插入,删除,遍历,查找,修改,最大(小)值,平均
    测试文档(final)
    详细设计文档(final)
    概要设计文档(final)
    需求规格说明书(final)
  • 原文地址:https://www.cnblogs.com/CJT-blog/p/10492633.html
Copyright © 2020-2023  润新知