函数递归
 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);
}
}

函数循环

 #include"BinaryTree.h"

 // ====================测试代码====================
// 测试完全二叉树:除了叶子节点,其他节点都有两个子节点
// 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(); return ;
}

测试代码

剑指offer——面试题27:二叉树的镜像的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. 剑指Offer:面试题25——二叉树中和为某一值的路径(java实现)

    问题描述: 输入一棵二叉树和一个整数,打印出二叉树中结点指的和为输入整数的所有路径.从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.二叉树结点的定义如下: public class Tree ...

  9. 剑指offer面试题27:二叉搜索树与双向链表

    题目:输入一颗二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的节点,只能调整树中节点指针的指向. 由于二叉搜索树是有序的,左子结点的值小于根节点的值,右子结点的值大于根节点的值 ...

随机推荐

  1. C语言基础第一次作业

    一,1)大学和高中最大的不同是没有人天天看着你,请问大学理想的师生关系是?有何感想? 看了邹欣老师博客中写到的教学基础——师生关系后陷入沉思,邹欣老师在她的博客中直接否认了传统认知的师生关系——蜡烛, ...

  2. nginx 集群介绍

    nginx 集群介绍 完成一次请求的步骤 1)用户发起请求 2)服务器接受请求 3)服务器处理请求(压力最大) 4)服务器响应请求 缺点:单点故障 单台服务器资源有限 单台服务器处理耗时长 ·1)部署 ...

  3. java消息中间件的使用与简介

    一.为什么要使用消息中间件 消息中间件就是可以省去繁琐的步骤,直达目的,怎么讲呢,就是比如你想很多人,知道你的动态,而知道的人可能手机没电,可能手机信号不好,可能手机不在服务区,或者看的人比较忙,看的 ...

  4. 用U盘安装Ubuntu主系统

    用U盘安装Ubuntu主系统 0. 学习Ubuntu系统的的动机 ubuntu系统的内核是linux系统,通过Ubuntu的学习,掌握lInux服务器搭建!!!! 硬件要求:闲置的笔记本  +  U盘 ...

  5. Receiving Transaction Processor Conundrum

    what would we do if we are faced with a situation to execute a receiving transaction in oracle ebusi ...

  6. ZooKeeper 一致性协议 ZAB 原理

    一致性协议有很多种,比如 Paxos,Raft,2PC,3PC等等,今天我们讲一种协议,ZAB 协议,该协议应该是所有一致性协议中生产环境中应用最多的了.为什么呢?因为他是为 Zookeeper 设计 ...

  7. J-Link eclipse Plug-ins install

    Quicklinks If you know what this is all about and you just need the update site details: name: GNU A ...

  8. Spring学习(六)——集成memcached客户端

    memcached是高性能的分布式内存缓存服务器.许多Web应用都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示. 但随着数据量的增大.访问的集中,就会出现RDBMS的负担加重.数据 ...

  9. netcore中使用bower还原出错的解决方法

    近期BitAdminCore框架在创建时,还原bower包出现502错 打开地址,发现原为是因为bower服务调整导致的. 果断处理: 1.通过管理员模式,启动命令行 2.进入npm所在目录 3.执行 ...

  10. C#多线程编程系列(一)- 简介

    目录 系列大纲 一.前言 二.目录结构 四.章节结构 五.相关链接 系列大纲 目前只整理到第二章,线程同步,笔者后面会慢慢更新,争取能把这本书中精华的知识都分享出来. C#多线程编程系列(一)- 简介 ...