【面试题十九】二叉树的镜像

原创 2013年12月02日 21:21:05

二叉树的镜像


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


MirrorTree.cpp:

#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
void Test1()
{
    printf("=====Test1 starts:=====\n");
    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=====\n");
    MirrorRecursively(pNode8);
    PrintTree(pNode8);

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

    DestroyTree(pNode8);
}

// 测试二叉树:出叶子结点之外,左右的结点都有且只有一个左子结点
//            8
//          7   
//        6 
//      5
//    4
void Test2()
{
    printf("=====Test2 starts:=====\n");
    BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
    BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);

    ConnectTreeNodes(pNode8, pNode7, NULL);
    ConnectTreeNodes(pNode7, pNode6, NULL);
    ConnectTreeNodes(pNode6, pNode5, NULL);
    ConnectTreeNodes(pNode5, pNode4, NULL);

    PrintTree(pNode8);

    printf("=====Test2: MirrorRecursively=====\n");
    MirrorRecursively(pNode8);
    PrintTree(pNode8);

    printf("=====Test2: MirrorIteratively=====\n");
    MirrorIteratively(pNode8);
    PrintTree(pNode8);

    DestroyTree(pNode8);
}

// 测试二叉树:出叶子结点之外,左右的结点都有且只有一个右子结点
//            8
//             7   
//              6 
//               5
//                4
void Test3()
{
    printf("=====Test3 starts:=====\n");
    BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
    BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);

    ConnectTreeNodes(pNode8, NULL, pNode7);
    ConnectTreeNodes(pNode7, NULL, pNode6);
    ConnectTreeNodes(pNode6, NULL, pNode5);
    ConnectTreeNodes(pNode5, NULL, pNode4);

    PrintTree(pNode8);

    printf("=====Test3: MirrorRecursively=====\n");
    MirrorRecursively(pNode8);
    PrintTree(pNode8);

    printf("=====Test3: MirrorIteratively=====\n");
    MirrorIteratively(pNode8);
    PrintTree(pNode8);

    DestroyTree(pNode8);
}

// 测试空二叉树:根结点为空指针
void Test4()
{
    printf("=====Test4 starts:=====\n");
    BinaryTreeNode* pNode = NULL;

    PrintTree(pNode);

    printf("=====Test4: MirrorRecursively=====\n");
    MirrorRecursively(pNode);
    PrintTree(pNode);

    printf("=====Test4: MirrorIteratively=====\n");
    MirrorIteratively(pNode);
    PrintTree(pNode);
}

// 测试只有一个结点的二叉树
void Test5()
{
    printf("=====Test5 starts:=====\n");
    BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);

    PrintTree(pNode8);

    printf("=====Test4: MirrorRecursively=====\n");
    MirrorRecursively(pNode8);
    PrintTree(pNode8);

    printf("=====Test4: MirrorIteratively=====\n");
    MirrorIteratively(pNode8);
    PrintTree(pNode8);
}


int main()
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();

    return 0;
}



BinaryTree.h:

#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:

#include <iostream>
#include "BinaryTree.h"
#include <cstdio>

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\n", pNode->m_nValue);

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

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

    printf("\n");
}

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:

.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.

=====Test2 starts:=====
value of this node is: 8
value of its left child is: 7.
right child is null.

value of this node is: 7
value of its left child is: 6.
right child is null.

value of this node is: 6
value of its left child is: 5.
right child is null.

value of this node is: 5
value of its left child is: 4.
right child is null.

value of this node is: 4
left child is null.
right child is null.

=====Test2: MirrorRecursively=====
value of this node is: 8
left child is null.
value of its right child is: 7.

value of this node is: 7
left child is null.
value of its right child is: 6.

value of this node is: 6
left child is null.
value of its right child is: 5.

value of this node is: 5
left child is null.
value of its right child is: 4.

value of this node is: 4
left child is null.
right child is null.

=====Test2: MirrorIteratively=====
value of this node is: 8
value of its left child is: 7.
right child is null.

value of this node is: 7
value of its left child is: 6.
right child is null.

value of this node is: 6
value of its left child is: 5.
right child is null.

value of this node is: 5
value of its left child is: 4.
right child is null.

value of this node is: 4
left child is null.
right child is null.

=====Test3 starts:=====
value of this node is: 8
left child is null.
value of its right child is: 7.

value of this node is: 7
left child is null.
value of its right child is: 6.

value of this node is: 6
left child is null.
value of its right child is: 5.

value of this node is: 5
left child is null.
value of its right child is: 4.

value of this node is: 4
left child is null.
right child is null.

=====Test3: MirrorRecursively=====
value of this node is: 8
left child is null.
value of its right child is: 7.

value of this node is: 7
left child is null.
value of its right child is: 6.

value of this node is: 6
left child is null.
value of its right child is: 5.

value of this node is: 5
left child is null.
value of its right child is: 4.

value of this node is: 4
left child is null.
right child is null.

=====Test3: MirrorIteratively=====
value of this node is: 8
value of its left child is: 7.
right child is null.

value of this node is: 7
value of its left child is: 6.
right child is null.

value of this node is: 6
value of its left child is: 5.
right child is null.

value of this node is: 5
value of its left child is: 4.
right child is null.

value of this node is: 4
left child is null.
right child is null.

=====Test4 starts:=====
this node is null.

=====Test4: MirrorRecursively=====
this node is null.

=====Test4: MirrorIteratively=====
this node is null.

=====Test5 starts:=====
value of this node is: 8
left child is null.
right child is null.

=====Test4: MirrorRecursively=====
value of this node is: 8
left child is null.
right child is null.

=====Test4: MirrorIteratively=====
value of this node is: 8
left child is null.
right child is null.


剑指offer系列之17:二叉树的镜像

问题描述:完成一个函数,输入一个二叉树,该函数输出它的镜像。思路:解决这样问题的时候,可以通过画图来让抽象的问题具体化,前序遍历这棵树的每个节点,如果遍历到的节点有子节点,就交换它的两个子节点,当交换...
  • ly_main
  • ly_main
  • 2016年04月12日 08:31
  • 107

剑指offer面试题 二叉树的镜像

题目描述:请完成一个函数,输入一个二叉树,该函数输出它的镜像。 解析: 代码如下:...
  • Andy710660541
  • Andy710660541
  • 2016年04月19日 08:58
  • 169

【剑指offer】面试题19:二叉树的镜像

题目:
  • a45872055555
  • a45872055555
  • 2014年06月15日 23:17
  • 452

剑指offer 面试题19:二叉树的镜像(leetcode226. Invert Binary Tree) 题解

面试题19 二叉树的镜像 提交地址: http://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=...
  • yanglr2010
  • yanglr2010
  • 2016年04月15日 21:19
  • 1152

剑指offer---二叉树的镜像---java实现

题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。  输入描述: 二叉树的镜像定义:源二叉树         8        /  \       6   10      / ...
  • snow_7
  • snow_7
  • 2016年07月04日 20:23
  • 2221

面试题17:二叉树的镜像

例如:下面两棵树互为镜像 思路:先序遍历树的每个结点,若遍历到的结点有子节点,则交换它的两个结点。 代码: #include "stdafx.h" #include using nam...
  • htyurencaotang
  • htyurencaotang
  • 2013年07月21日 21:39
  • 4185

剑指offer面试题:求二叉树的镜像(递归、循环解法及测试用例)

题目:给定二叉树,将其变换为源二叉树的镜像。 二叉树的定义如下: struct TreeNode {     int val;     TreeNode* left;     TreeNode* ...
  • yanxiaolx
  • yanxiaolx
  • 2016年07月25日 01:34
  • 1681

二叉树的镜像[剑指offer]之python实现

题目描述操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10...
  • huaxixidongbeishida
  • huaxixidongbeishida
  • 2016年10月26日 16:44
  • 596

笔试面试算法经典--二叉树的镜像-递归与非递归实现(Java)

给出一棵二叉树,求它的镜像,如下图:右边是二叉树是左边二叉树的镜像。解法1(递归)思路1:如果当前节点为空,返回,否则交换该节点的左右节点,递归的对其左右节点进行交换处理。public static ...
  • u013309870
  • u013309870
  • 2017年04月10日 19:04
  • 1025

二叉树的五道面试题

1、判断一棵树是否是完全二叉树; 2、求二叉树中最远两个结点的距离; 3、由前序和中序遍历序列重建二叉树 (前序序列:1 2 3 4 5 6 - 中序序列:3 2 4 1 6 5); 4、求二叉...
  • LLZK_
  • LLZK_
  • 2016年11月30日 10:14
  • 879
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【面试题十九】二叉树的镜像
举报原因:
原因补充:

(最多只允许输入30个字)