剑指offer——面试题27:二叉树的镜像
- 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:二叉树的镜像的更多相关文章
- 剑指Offer:面试题19——二叉树的镜像(java实现)
问题描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树结点定义为: public class TreeNode { int val = 0; TreeNode left = null; Tr ...
- 剑指offer面试题19 二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- 剑指Offer - 九度1521 - 二叉树的镜像
剑指Offer - 九度1521 - 二叉树的镜像2013-11-30 23:32 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入 ...
- 剑指offer:对称的二叉树(镜像,递归,非递归DFS栈+BFS队列)
1. 题目描述 /** 请实现一个函数,用来判断一颗二叉树是不是对称的. 注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的 */ 2. 递归 思路: /** 1.只要pRoot.left和 ...
- 剑指offer(18)二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- 剑指offer十八之二叉树的镜像
一.题目 操作给定的二叉树,将其变换为源二叉树的镜像.二叉树的镜像定义: 源二叉树 : 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树: 8 / \ 10 6 / \ ...
- 【剑指Offer】18、二叉树的镜像
题目描述: 操作给定的二叉树,将其变换为原二叉树的镜像. 解题思路: 求一棵树的镜像的过程:先前序遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子结点.当交换完所有的非 ...
- 剑指Offer:面试题25——二叉树中和为某一值的路径(java实现)
问题描述: 输入一棵二叉树和一个整数,打印出二叉树中结点指的和为输入整数的所有路径.从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.二叉树结点的定义如下: public class Tree ...
- 剑指offer面试题27:二叉搜索树与双向链表
题目:输入一颗二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的节点,只能调整树中节点指针的指向. 由于二叉搜索树是有序的,左子结点的值小于根节点的值,右子结点的值大于根节点的值 ...
随机推荐
- 基于Web Service的客户端框架搭建二:数据转换层(FCL)
引言 要使用WebService来分离客户端与服务端,必定要使用约定好两者之间的数据契约.Json数据以其完全独立于语言的优势,成为开发者的首选.C# JavaScriptSerializer为Jso ...
- 整理悬浮在列表中a元素时改变a元素上下边框颜色的问题。
整理一下当悬浮在a元素上时a的上下边颜色改变,并且里面的内容不会移动,下面是PSD图效果区域: 刚开始我先给A元素加了上下边框和颜色,利用a:hover改变a元素上下的边框颜色,但是第一个a元素的下边 ...
- swoole集群 nginx配置
nginx配置文件: upstream cat { server 192.168.149.133:9502 weight=5; server 192.168.149.134:9502 weight=5 ...
- 20155318 2016-2017-2 《Java程序设计》第九学习总结
20155318 2016-2017-2 <Java程序设计>第九学习总结 教材学习内容总结 学习目标 了解JDBC架构 掌握JDBC架构 掌握反射与ClassLoader 了解自定义泛型 ...
- java并发编程实战:第七章----取消与关闭
Java没有提供任何机制来安全地终止线程(虽然Thread.stop和suspend方法提供了这样的机制,但由于存在缺陷,因此应该避免使用 中断:一种协作机制,能够使一个线程终止另一个线程的当前工作 ...
- 5、Docker架构和底层技术
5.1 Docker Platform Docker提供了一个开发,打包,运行APP的平台 把APP和底层infrastructure隔离开来 5.2 Docker Engine 后台进程(docke ...
- 简单配置vps,防ddos攻击
防人之心不可无. 网上总有些无聊或者有意的人.不多说了.上干货,配置vps apf防小流量ddos攻击. 对于大流量的ddos攻击, 需要机房的硬件防火墙,vps内部可能也扛不住. 1. 安装 DDo ...
- CDI Event解析
CDI(Contexts And Dependency Injection)是JavaEE 6标准中一个规范,将依赖注入IOC/DI上升到容器级别, 它提供了Java EE平台上服务注入的组件管理核心 ...
- Discuz showmessage函数解析[转]
函数相关文件 \source\function\function_core.php\source\function\function_message.php ## 函数解释 /** * 显示提示信息 ...
- sqlserver常用函数
1.字符串函数 --ascii函数,返回字符串最左侧字符的ascii码值 SELECT ASCII('dsd') AS asciistr --ascii代码转换函数,返回指定ascii值对应的字符 ) ...