// 面试题27:二叉树的镜像
// 题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像。 #include <iostream>
#include "BinaryTree.h"
#include <stack> void MirrorRecursively(BinaryTreeNode *pNode)//递归算法(自下而上)
{
if ((pNode == nullptr) || (pNode->m_pLeft == nullptr && pNode->m_pRight == nullptr))//鲁棒性
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 == nullptr)
return; std::stack<BinaryTreeNode*> stackTreeNode;//建立一个栈
stackTreeNode.push(pRoot);//压入根节点 while (stackTreeNode.size() > )//当栈内有元素时
{
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();
BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
BinaryTreeNode* pNode10 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode();
BinaryTreeNode* pNode9 = CreateBinaryTreeNode();
BinaryTreeNode* pNode11 = CreateBinaryTreeNode(); 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
//
void Test2()
{
printf("=====Test2 starts:=====\n");
BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode();
BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode8, pNode7, nullptr);
ConnectTreeNodes(pNode7, pNode6, nullptr);
ConnectTreeNodes(pNode6, pNode5, nullptr);
ConnectTreeNodes(pNode5, pNode4, nullptr); 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();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode();
BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode8, nullptr, pNode7);
ConnectTreeNodes(pNode7, nullptr, pNode6);
ConnectTreeNodes(pNode6, nullptr, pNode5);
ConnectTreeNodes(pNode5, nullptr, 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 = nullptr; 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(); PrintTree(pNode8); printf("=====Test4: MirrorRecursively=====\n");
MirrorRecursively(pNode8);
PrintTree(pNode8); printf("=====Test4: MirrorIteratively=====\n");
MirrorIteratively(pNode8);
PrintTree(pNode8);
} int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
system("pause");
return ;
}

《剑指offer》第二十七题(二叉树的镜像)的更多相关文章

  1. 剑指Offer - 九度1521 - 二叉树的镜像

    剑指Offer - 九度1521 - 二叉树的镜像2013-11-30 23:32 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入 ...

  2. 剑指offer:对称的二叉树(镜像,递归,非递归DFS栈+BFS队列)

    1. 题目描述 /** 请实现一个函数,用来判断一颗二叉树是不是对称的. 注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的 */ 2. 递归 思路: /** 1.只要pRoot.left和 ...

  3. 剑指Offer:面试题19——二叉树的镜像(java实现)

    问题描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树结点定义为: public class TreeNode { int val = 0; TreeNode left = null; Tr ...

  4. 剑指offer(18)二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  5. 剑指offer十八之二叉树的镜像

    一.题目 操作给定的二叉树,将其变换为源二叉树的镜像.二叉树的镜像定义:        源二叉树 : 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树: 8 / \ 10 6 / \ ...

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

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像.  输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  7. 【剑指Offer】18、二叉树的镜像

      题目描述:   操作给定的二叉树,将其变换为原二叉树的镜像.   解题思路:   求一棵树的镜像的过程:先前序遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子结点.当交换完所有的非 ...

  8. 剑指offer第二版-8.二叉树的下一个节点

    描述:给定一棵二叉树和其中的一个节点,找出中序遍历序列的下一个节点.树中应定义指向左节点.右节点.父节点的三个变量. 思路: 1.如果输入的当前节点有右孩子,则它的下一个节点即为该右孩子为根节点的子树 ...

  9. 剑指offer五十七之二叉树的下一个结点

    一.题目 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 二.思路 结合图,我们可发现分成两大类: 1.有右子树 ...

  10. 剑指Offer(十七):树的子结构

    剑指Offer(十七):树的子结构 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/baidu_ ...

随机推荐

  1. try except else

    try except 语句还有一个可选的else子句,如果使用这个子句,那么必须放在所有的except子句之后.这个子句将在try子句没有发生任何异常的时候执行.例如: for arg in sys. ...

  2. 003-ubuntu上安装mysql

    安装如下: 1.安装服务端:# sudo apt-get  install mysql-server. 2.安装客户端:# sudo apt-get -y install mysql-server. ...

  3. 20154312《网络对抗》Exp2 后门原理与实践

    常见问题快速链接 Handler failed to bind to xxx.xxx.xx.xxx:xxxx 使用Webcam_snap命令提示1411错误,无法正常拍照 常用后门工具实践 Windo ...

  4. linux常用命令:chmod 命令

    chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法. Linux系统中的每 ...

  5. python中有两个下划线__的是内置方法,一个下划线_或者没有下划线的可能是属性,也可能是方法,也可能是类名

    python中有两个下划线__的是内置方法,一个下划线_或者没有下划线的可能是属性,也可能是方法,也可能是类名,如果在类中定义的就是类的私有成员. >>> dir(__builtin ...

  6. IO(基础知识)

        IO流类的构造方法决定是输入流还是输出流.输入流连接一个文件,它会将文件中的内容读到流里面,read方法是将流里面的内容     往外读.输出流连接一个文件,它的write方法,是将内存中的内 ...

  7. mysql日志文件目录

    默认情况下mysql的二进制日志文件保存在默认的数据目录data下,如:/usr/local/mysql/data 修改日志保存目录(/backup/mysqlbinlog/mysql-bin)的话: ...

  8. UVA10298 Power Strings

    UVA10298 Power Strings hash+乘法逆元+一点点数学知识 我们用取余法算出主串的hash,然后从小到大枚举子串的长度 显然,如果若干个子串的复制的hash值之和等于主串的has ...

  9. NFS Iptables放行服务端口

    启动NFS会开启如下端口:1)portmapper 端口:111 udp/tcp:2)nfs/nfs_acl 端口:2049 udp/tcp:3)mountd 端口:"32768--6553 ...

  10. 利用.bat(批处理)来删除KEIL编译生成的无用文件

    新建一个.txt文件. 在里面输入如下内容: del *.bak /s del *.ddk /s del *.edk /s del *.lst /s del *.lnp /s del *.mpf /s ...