函数递归
  1. void MirrorIteratively(BinaryTreeNode* pRoot)
  2. {
  3. if(pRoot == nullptr)
  4. return;
  5.  
  6. std::stack<BinaryTreeNode*> stackTreeNode;
  7. stackTreeNode.push(pRoot);
  8.  
  9. while(stackTreeNode.size() > )
  10. {
  11. BinaryTreeNode *pNode = stackTreeNode.top();
  12. stackTreeNode.pop();
  13.  
  14. BinaryTreeNode *pTemp = pNode->m_pLeft;
  15. pNode->m_pLeft = pNode->m_pRight;
  16. pNode->m_pRight = pTemp;
  17.  
  18. if(pNode->m_pLeft)
  19. stackTreeNode.push(pNode->m_pLeft);
  20.  
  21. if(pNode->m_pRight)
  22. stackTreeNode.push(pNode->m_pRight);
  23. }
  24. }

函数循环

  1. #include"BinaryTree.h"
  2.  
  3. // ====================测试代码====================
  4. // 测试完全二叉树:除了叶子节点,其他节点都有两个子节点
  5. // 8
  6. // 6 10
  7. // 5 7 9 11
  8. void Test1()
  9. {
  10. printf("=====Test1 starts:=====\n");
  11. BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
  12. BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
  13. BinaryTreeNode* pNode10 = CreateBinaryTreeNode();
  14. BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
  15. BinaryTreeNode* pNode7 = CreateBinaryTreeNode();
  16. BinaryTreeNode* pNode9 = CreateBinaryTreeNode();
  17. BinaryTreeNode* pNode11 = CreateBinaryTreeNode();
  18.  
  19. ConnectTreeNodes(pNode8, pNode6, pNode10);
  20. ConnectTreeNodes(pNode6, pNode5, pNode7);
  21. ConnectTreeNodes(pNode10, pNode9, pNode11);
  22.  
  23. PrintTree(pNode8);
  24.  
  25. printf("=====Test1: MirrorRecursively=====\n");
  26. MirrorRecursively(pNode8);
  27. PrintTree(pNode8);
  28.  
  29. printf("=====Test1: MirrorIteratively=====\n");
  30. MirrorIteratively(pNode8);
  31. PrintTree(pNode8);
  32.  
  33. DestroyTree(pNode8);
  34. }
  35.  
  36. // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个左子结点
  37. // 8
  38. // 7
  39. // 6
  40. // 5
  41. //
  42. void Test2()
  43. {
  44. printf("=====Test2 starts:=====\n");
  45. BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
  46. BinaryTreeNode* pNode7 = CreateBinaryTreeNode();
  47. BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
  48. BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
  49. BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
  50.  
  51. ConnectTreeNodes(pNode8, pNode7, nullptr);
  52. ConnectTreeNodes(pNode7, pNode6, nullptr);
  53. ConnectTreeNodes(pNode6, pNode5, nullptr);
  54. ConnectTreeNodes(pNode5, pNode4, nullptr);
  55.  
  56. PrintTree(pNode8);
  57.  
  58. printf("=====Test2: MirrorRecursively=====\n");
  59. MirrorRecursively(pNode8);
  60. PrintTree(pNode8);
  61.  
  62. printf("=====Test2: MirrorIteratively=====\n");
  63. MirrorIteratively(pNode8);
  64. PrintTree(pNode8);
  65.  
  66. DestroyTree(pNode8);
  67. }
  68.  
  69. // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个右子结点
  70. // 8
  71. // 7
  72. // 6
  73. // 5
  74. // 4
  75. void Test3()
  76. {
  77. printf("=====Test3 starts:=====\n");
  78. BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
  79. BinaryTreeNode* pNode7 = CreateBinaryTreeNode();
  80. BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
  81. BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
  82. BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
  83.  
  84. ConnectTreeNodes(pNode8, nullptr, pNode7);
  85. ConnectTreeNodes(pNode7, nullptr, pNode6);
  86. ConnectTreeNodes(pNode6, nullptr, pNode5);
  87. ConnectTreeNodes(pNode5, nullptr, pNode4);
  88.  
  89. PrintTree(pNode8);
  90.  
  91. printf("=====Test3: MirrorRecursively=====\n");
  92. MirrorRecursively(pNode8);
  93. PrintTree(pNode8);
  94.  
  95. printf("=====Test3: MirrorIteratively=====\n");
  96. MirrorIteratively(pNode8);
  97. PrintTree(pNode8);
  98.  
  99. DestroyTree(pNode8);
  100. }
  101.  
  102. // 测试空二叉树:根结点为空指针
  103. void Test4()
  104. {
  105. printf("=====Test4 starts:=====\n");
  106. BinaryTreeNode* pNode = nullptr;
  107.  
  108. PrintTree(pNode);
  109.  
  110. printf("=====Test4: MirrorRecursively=====\n");
  111. MirrorRecursively(pNode);
  112. PrintTree(pNode);
  113.  
  114. printf("=====Test4: MirrorIteratively=====\n");
  115. MirrorIteratively(pNode);
  116. PrintTree(pNode);
  117. }
  118.  
  119. // 测试只有一个结点的二叉树
  120. void Test5()
  121. {
  122. printf("=====Test5 starts:=====\n");
  123. BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
  124.  
  125. PrintTree(pNode8);
  126.  
  127. printf("=====Test4: MirrorRecursively=====\n");
  128. MirrorRecursively(pNode8);
  129. PrintTree(pNode8);
  130.  
  131. printf("=====Test4: MirrorIteratively=====\n");
  132. MirrorIteratively(pNode8);
  133. PrintTree(pNode8);
  134. }
  135.  
  136. int main(int argc, char* argv[])
  137. {
  138. Test1();
  139. Test2();
  140. Test3();
  141. Test4();
  142. Test5();
  143.  
  144. return ;
  145. }

测试代码

剑指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. 基于Web Service的客户端框架搭建二:数据转换层(FCL)

    引言 要使用WebService来分离客户端与服务端,必定要使用约定好两者之间的数据契约.Json数据以其完全独立于语言的优势,成为开发者的首选.C# JavaScriptSerializer为Jso ...

  2. 整理悬浮在列表中a元素时改变a元素上下边框颜色的问题。

    整理一下当悬浮在a元素上时a的上下边颜色改变,并且里面的内容不会移动,下面是PSD图效果区域: 刚开始我先给A元素加了上下边框和颜色,利用a:hover改变a元素上下的边框颜色,但是第一个a元素的下边 ...

  3. swoole集群 nginx配置

    nginx配置文件: upstream cat { server 192.168.149.133:9502 weight=5; server 192.168.149.134:9502 weight=5 ...

  4. 20155318 2016-2017-2 《Java程序设计》第九学习总结

    20155318 2016-2017-2 <Java程序设计>第九学习总结 教材学习内容总结 学习目标 了解JDBC架构 掌握JDBC架构 掌握反射与ClassLoader 了解自定义泛型 ...

  5. java并发编程实战:第七章----取消与关闭

    Java没有提供任何机制来安全地终止线程(虽然Thread.stop和suspend方法提供了这样的机制,但由于存在缺陷,因此应该避免使用 中断:一种协作机制,能够使一个线程终止另一个线程的当前工作 ...

  6. 5、Docker架构和底层技术

    5.1 Docker Platform Docker提供了一个开发,打包,运行APP的平台 把APP和底层infrastructure隔离开来 5.2 Docker Engine 后台进程(docke ...

  7. 简单配置vps,防ddos攻击

    防人之心不可无. 网上总有些无聊或者有意的人.不多说了.上干货,配置vps apf防小流量ddos攻击. 对于大流量的ddos攻击, 需要机房的硬件防火墙,vps内部可能也扛不住. 1. 安装 DDo ...

  8. CDI Event解析

    CDI(Contexts And Dependency Injection)是JavaEE 6标准中一个规范,将依赖注入IOC/DI上升到容器级别, 它提供了Java EE平台上服务注入的组件管理核心 ...

  9. Discuz showmessage函数解析[转]

    函数相关文件 \source\function\function_core.php\source\function\function_message.php ## 函数解释 /** * 显示提示信息 ...

  10. sqlserver常用函数

    1.字符串函数 --ascii函数,返回字符串最左侧字符的ascii码值 SELECT ASCII('dsd') AS asciistr --ascii代码转换函数,返回指定ascii值对应的字符 ) ...