zoukankan      html  css  js  c++  java
  • 【面试题019】二叉树的镜像

    【面试题019】二叉树的镜像 

     

    这道题很简单就是交换结点,利用递归解解决问题思路清晰,代码简洁;

    MirrorTree.cpp:

    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
     
    #include <iostream>
    #include <cstdio>
    #include <stack>
    #include "BinaryTree.h"

    using namespace std;

    //递归
    void MirrorRecursively(BinaryTreeNode *pNode)
    {
        if((pNode == NULL) || (pNode->m_pLeft == NULL && pNode->m_pRight))
        {
            return ;
        }

        BinaryTreeNode *pTemp = pNode->m_pLeft;
        pNode->m_pLeft = pNode->m_pRight;
        pNode->m_pRight = pTemp;

        if(pNode->m_pLeft)
        {
            MirrorRecursively(pNode->m_pLeft);
        }
        if(pNode->m_pRight)
        {
            MirrorRecursively(pNode->m_pRight);
        }
    }
    //迭代
    void MirrorIteratively(BinaryTreeNode *pRoot)
    {
        if(pRoot == NULL)
        {
            return ;
        }

        stack<BinaryTreeNode *> stackTreeNode;
        stackTreeNode.push(pRoot);

        while(stackTreeNode.size() > 0)
        {
            BinaryTreeNode *pNode = stackTreeNode.top();
            stackTreeNode.pop();

            BinaryTreeNode *pTemp = pNode->m_pLeft;
            pNode->m_pLeft = pNode->m_pRight;
            pNode->m_pRight = pTemp;

            if(pNode->m_pLeft)
            {
                stackTreeNode.push(pNode->m_pLeft);
            }
            if(pNode->m_pRight)
            {
                stackTreeNode.push(pNode->m_pRight);
            }
        }
    }

    // ====================测试代码====================
    // 测试完全二叉树:除了叶子节点,其他节点都有两个子节点
    //            8
    //        6      10
    //       5 7    9  11
    int main()
    {
        printf("=====Test1 starts:===== ");
        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);

        PrintTree(pNode8);

        printf("=====Test1: MirrorRecursively===== ");
        MirrorRecursively(pNode8);
        PrintTree(pNode8);

        printf("=====Test1: MirrorIteratively===== ");
        MirrorIteratively(pNode8);
        PrintTree(pNode8);

        DestroyTree(pNode8);
        return 0;
    }

    BinaryTree.h:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
     
    #ifndef _BINARY_TREE_H_
    #define _BINARY_TREE_H_

    struct BinaryTreeNode
    {
        int                    m_nValue;
        BinaryTreeNode        *m_pLeft;
        BinaryTreeNode        *m_pRight;
    };

    BinaryTreeNode *CreateBinaryTreeNode(int value);
    void ConnectTreeNodes(
        BinaryTreeNode *pParent,
        BinaryTreeNode *pLeft, BinaryTreeNode *pRight);
    void PrintTreeNode(BinaryTreeNode *pNode);
    void PrintTree(BinaryTreeNode *pRoot);
    void DestroyTree(BinaryTreeNode *pRoot);


    #endif /*_BINARY_TREE_H_*/

     

    BinaryTree.cpp:

    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
     
    #include <iostream>
    #include <cstdio>
    #include "BinaryTree.h"

    using namespace std;
    BinaryTreeNode *CreateBinaryTreeNode(int value)
    {
        BinaryTreeNode *pNode = new BinaryTreeNode();
        pNode->m_nValue = value;
        pNode->m_pLeft = NULL;
        pNode->m_pRight = NULL;

        return pNode;
    }

    void ConnectTreeNodes(
        BinaryTreeNode *pParent,
        BinaryTreeNode *pLeft,
        BinaryTreeNode *pRight)
    {
        if(pParent != NULL)
        {
            pParent->m_pLeft = pLeft;
            pParent->m_pRight = pRight;
        }
    }

    void PrintTreeNode(BinaryTreeNode *pNode)
    {
        if(pNode != NULL)
        {
            printf("value of this node is: %d ", pNode->m_nValue);

            if(pNode->m_pLeft != NULL)
                printf("value of its left child is: %d. ",
                       pNode->m_pLeft->m_nValue);
            else
                printf("left child is null. ");

            if(pNode->m_pRight != NULL)
                printf("value of its right child is: %d. ",
                       pNode->m_pRight->m_nValue);
            else
                printf("right child is null. ");
        }
        else
        {
            printf("this node is null. ");
        }

        printf(" ");
    }

    void PrintTree(BinaryTreeNode *pRoot)
    {
        PrintTreeNode(pRoot);

        if(pRoot != NULL)
        {
            if(pRoot->m_pLeft != NULL)
                PrintTree(pRoot->m_pLeft);

            if(pRoot->m_pRight != NULL)
                PrintTree(pRoot->m_pRight);
        }
    }

    void DestroyTree(BinaryTreeNode *pRoot)
    {
        if(pRoot != NULL)
        {
            BinaryTreeNode *pLeft = pRoot->m_pLeft;
            BinaryTreeNode *pRight = pRoot->m_pRight;

            delete pRoot;
            pRoot = NULL;

            DestroyTree(pLeft);
            DestroyTree(pRight);
        }
    }

    Makefile:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    .PHONY:clean  
    CPP=g++  
    CFLAGS=-Wall -g  
    BIN=test  
    OBJS=MirrorTree.o BinaryTree.o  
    LIBS=  
    $(BIN):$(OBJS)  
        $(CPP) $(CFLAGS) $^ -o $@ $(LIBS)  
    %.o:%.cpp  
        $(CPP) $(CFLAGS) -c $< -o $@  
    clean:  
        rm -f *.o $(BIN)  



    运行结果:

    =====Test1 starts:=====
    value of this node is: 8
    value of its left child is: 6.
    value of its right child is: 10.
    value of this node is: 6
    value of its left child is: 5.
    value of its right child is: 7.
    value of this node is: 5
    left child is null.
    right child is null.
    value of this node is: 7
    left child is null.
    right child is null.
    value of this node is: 10
    value of its left child is: 9.
    value of its right child is: 11.
    value of this node is: 9
    left child is null.
    right child is null.
    value of this node is: 11
    left child is null.
    right child is null.
    =====Test1: MirrorRecursively=====
    value of this node is: 8
    value of its left child is: 10.
    value of its right child is: 6.
    value of this node is: 10
    value of its left child is: 11.
    value of its right child is: 9.
    value of this node is: 11
    left child is null.
    right child is null.
    value of this node is: 9
    left child is null.
    right child is null.
    value of this node is: 6
    value of its left child is: 7.
    value of its right child is: 5.
    value of this node is: 7
    left child is null.
    right child is null.
    value of this node is: 5
    left child is null.
    right child is null.
    =====Test1: MirrorIteratively=====
    value of this node is: 8
    value of its left child is: 6.
    value of its right child is: 10.
    value of this node is: 6
    value of its left child is: 5.
    value of its right child is: 7.
    value of this node is: 5
    left child is null.
    right child is null.
    value of this node is: 7
    left child is null.
    right child is null.
    value of this node is: 10
    value of its left child is: 9.
    value of its right child is: 11.
    value of this node is: 9
    left child is null.
    right child is null.
    value of this node is: 11
    left child is null.
    right child is null.
     
  • 相关阅读:
    .NET的JSNO 序列化跟反序列化
    SQL Server 查询分析器提供的所有键盘快捷方式(转)
    SQL Server 中WITH (NOLOCK)浅析(转潇湘隐者)
    Microsoft Dynamics CRM 2011的组织服务中的RetrieveMultiple方法(转)
    C#对多个集合和数组的操作(合并,去重,判断)
    Silverlight Telerik控件学习:主题Theme切换html教程
    VMware 11安装Mac OS X 10.10
    Android 下载网络图片注意的问题
    对下载文件是否完整的判断方法
    Android实现通用的ActivityGroup(效果类似Android微博客户端主界面),强烈建议不要再使用TabActivity
  • 原文地址:https://www.cnblogs.com/codemylife/p/3715765.html
Copyright © 2011-2022 走看看