• 《剑指offer》第三十二题(之字形打印二叉树)


    // 面试题32(三):之字形打印二叉树
    // 题目:请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺
    // 序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,
    // 其他行以此类推。
    
    #include <iostream>
    #include "BinaryTree.h"
    #include <stack>
    
    void Print(BinaryTreeNode* pRoot)
    {
        if (pRoot == nullptr)
            return;
    
        std::stack<BinaryTreeNode*> levels[2];//两个栈
        int current = 0;//设置两个0/1分布但不等的值,用来轮到作为当前和下个栈
        int next = 1;
    
        levels[current].push(pRoot);
        while (!levels[0].empty() || !levels[1].empty())
        {
            BinaryTreeNode* pNode = levels[current].top();
            levels[current].pop();
    
            printf("%d ", pNode->m_nValue);
    
            if (current == 0)//决定先左还是先右
            {
                if (pNode->m_pLeft != nullptr)
                    levels[next].push(pNode->m_pLeft);
                if (pNode->m_pRight != nullptr)
                    levels[next].push(pNode->m_pRight);
            }
            else
            {
                if (pNode->m_pRight != nullptr)
                    levels[next].push(pNode->m_pRight);
                if (pNode->m_pLeft != nullptr)
                    levels[next].push(pNode->m_pLeft);
            }
    
            if (levels[current].empty())//如果当前栈空了,就换一下栈的序号
            {
                printf("
    ");
                current = 1 - current;
                next = 1 - next;
            }
        }
    }
    
    // ====================测试代码====================
    //            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("10 6 
    ");
        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("
    ");
    }
    
    //                8
    //        4              12
    //     2     6       10      14
    //   1  3  5  7     9 11   13  15
    void Test7()
    {
        BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
        BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);
        BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
        BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
        BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
        BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14);
        BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
        BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
        BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
        BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
        BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);
        BinaryTreeNode* pNode13 = CreateBinaryTreeNode(13);
        BinaryTreeNode* pNode15 = CreateBinaryTreeNode(15);
    
        ConnectTreeNodes(pNode8, pNode4, pNode12);
        ConnectTreeNodes(pNode4, pNode2, pNode6);
        ConnectTreeNodes(pNode12, pNode10, pNode14);
        ConnectTreeNodes(pNode2, pNode1, pNode3);
        ConnectTreeNodes(pNode6, pNode5, pNode7);
        ConnectTreeNodes(pNode10, pNode9, pNode11);
        ConnectTreeNodes(pNode14, pNode13, pNode15);
    
        printf("====Test7 Begins: ====
    ");
        printf("Expected Result is:
    ");
        printf("8 
    ");
        printf("12 4 
    ");
        printf("2 6 10 14 
    ");
        printf("15 13 11 9 7 5 3 1 
    
    ");
    
        printf("Actual Result is: 
    ");
        Print(pNode8);
        printf("
    ");
    
        DestroyTree(pNode8);
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
        Test6();
        Test7();
        system("pause");
        return 0;
    }
  • 相关阅读:
    methodForSelector
    判定一个点P是否存在于指定的三角形ABC内
    xcode error failed to launch no such file or directory
    【转】SQLServer系统变量使用
    【转】SQL SERVER中查询某个表或某个索引是否存在
    wtforms
    Flask上下文管理、session原理和全局g对象
    Oldboy s4 Flask
    批处理删除文件夹下所有文件和文件夹
    autojs使用
  • 原文地址:https://www.cnblogs.com/CJT-blog/p/10493212.html
Copyright © 2020-2023  润新知