1. // 面试题28:对称的二叉树
  2. // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和
  3. // 它的镜像一样,那么它是对称的。
  4.  
  5. #include <iostream>
  6. #include "BinaryTree.h"
  7.  
  8. bool isSymmetrical(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2);
  9.  
  10. bool isSymmetrical(BinaryTreeNode* pRoot)
  11. {
  12. return isSymmetrical(pRoot, pRoot);
  13. }
  14.  
  15. bool isSymmetrical(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2)
  16. {
  17. if (pRoot1 == nullptr && pRoot2 == nullptr)//当二者都为空,true
  18. return true;
  19.  
  20. if (pRoot1 == nullptr || pRoot2 == nullptr)//只有一个为空,flase
  21. return false;
  22.  
  23. if (pRoot1->m_nValue != pRoot2->m_nValue)//当二者值不等,flase
  24. return false;
  25.  
  26. return isSymmetrical(pRoot1->m_pLeft, pRoot2->m_pRight)
  27. && isSymmetrical(pRoot1->m_pRight, pRoot2->m_pLeft);//看看你镜像和我是不是一样~
  28. }//注意这里有个关键是,要测试是否都是空的,详见test9和10
  29.  
  30. // ====================测试代码====================
  31. void Test(const char* testName, BinaryTreeNode* pRoot, bool expected)
  32. {
  33. if (testName != nullptr)
  34. printf("%s begins: ", testName);
  35.  
  36. if (isSymmetrical(pRoot) == expected)
  37. printf("Passed.\n");
  38. else
  39. printf("FAILED.\n");
  40. }
  41.  
  42. // 8
  43. // 6 6
  44. // 5 7 7 5
  45. void Test1()
  46. {
  47. BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
  48. BinaryTreeNode* pNode61 = CreateBinaryTreeNode();
  49. BinaryTreeNode* pNode62 = CreateBinaryTreeNode();
  50. BinaryTreeNode* pNode51 = CreateBinaryTreeNode();
  51. BinaryTreeNode* pNode71 = CreateBinaryTreeNode();
  52. BinaryTreeNode* pNode72 = CreateBinaryTreeNode();
  53. BinaryTreeNode* pNode52 = CreateBinaryTreeNode();
  54.  
  55. ConnectTreeNodes(pNode8, pNode61, pNode62);
  56. ConnectTreeNodes(pNode61, pNode51, pNode71);
  57. ConnectTreeNodes(pNode62, pNode72, pNode52);
  58.  
  59. Test("Test1", pNode8, true);
  60.  
  61. DestroyTree(pNode8);
  62. }
  63.  
  64. // 8
  65. // 6 9
  66. // 5 7 7 5
  67. void Test2()
  68. {
  69. BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
  70. BinaryTreeNode* pNode61 = CreateBinaryTreeNode();
  71. BinaryTreeNode* pNode9 = CreateBinaryTreeNode();
  72. BinaryTreeNode* pNode51 = CreateBinaryTreeNode();
  73. BinaryTreeNode* pNode71 = CreateBinaryTreeNode();
  74. BinaryTreeNode* pNode72 = CreateBinaryTreeNode();
  75. BinaryTreeNode* pNode52 = CreateBinaryTreeNode();
  76.  
  77. ConnectTreeNodes(pNode8, pNode61, pNode9);
  78. ConnectTreeNodes(pNode61, pNode51, pNode71);
  79. ConnectTreeNodes(pNode9, pNode72, pNode52);
  80.  
  81. Test("Test2", pNode8, false);
  82.  
  83. DestroyTree(pNode8);
  84. }
  85.  
  86. // 8
  87. // 6 6
  88. // 5 7 7
  89. void Test3()
  90. {
  91. BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
  92. BinaryTreeNode* pNode61 = CreateBinaryTreeNode();
  93. BinaryTreeNode* pNode62 = CreateBinaryTreeNode();
  94. BinaryTreeNode* pNode51 = CreateBinaryTreeNode();
  95. BinaryTreeNode* pNode71 = CreateBinaryTreeNode();
  96. BinaryTreeNode* pNode72 = CreateBinaryTreeNode();
  97.  
  98. ConnectTreeNodes(pNode8, pNode61, pNode62);
  99. ConnectTreeNodes(pNode61, pNode51, pNode71);
  100. ConnectTreeNodes(pNode62, pNode72, nullptr);
  101.  
  102. Test("Test3", pNode8, false);
  103.  
  104. DestroyTree(pNode8);
  105. }
  106.  
  107. // 5
  108. // / \
  109. // 3 3
  110. // / \
  111. // 4 4
  112. // / \
  113. // 2 2
  114. // / \
  115. // 1 1
  116. void Test4()
  117. {
  118. BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
  119. BinaryTreeNode* pNode31 = CreateBinaryTreeNode();
  120. BinaryTreeNode* pNode32 = CreateBinaryTreeNode();
  121. BinaryTreeNode* pNode41 = CreateBinaryTreeNode();
  122. BinaryTreeNode* pNode42 = CreateBinaryTreeNode();
  123. BinaryTreeNode* pNode21 = CreateBinaryTreeNode();
  124. BinaryTreeNode* pNode22 = CreateBinaryTreeNode();
  125. BinaryTreeNode* pNode11 = CreateBinaryTreeNode();
  126. BinaryTreeNode* pNode12 = CreateBinaryTreeNode();
  127.  
  128. ConnectTreeNodes(pNode5, pNode31, pNode32);
  129. ConnectTreeNodes(pNode31, pNode41, nullptr);
  130. ConnectTreeNodes(pNode32, nullptr, pNode42);
  131. ConnectTreeNodes(pNode41, pNode21, nullptr);
  132. ConnectTreeNodes(pNode42, nullptr, pNode22);
  133. ConnectTreeNodes(pNode21, pNode11, nullptr);
  134. ConnectTreeNodes(pNode22, nullptr, pNode12);
  135.  
  136. Test("Test4", pNode5, true);
  137.  
  138. DestroyTree(pNode5);
  139. }
  140.  
  141. // 5
  142. // / \
  143. // 3 3
  144. // / \
  145. // 4 4
  146. // / \
  147. // 6 2
  148. // / \
  149. // 1 1
  150. void Test5()
  151. {
  152. BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
  153. BinaryTreeNode* pNode31 = CreateBinaryTreeNode();
  154. BinaryTreeNode* pNode32 = CreateBinaryTreeNode();
  155. BinaryTreeNode* pNode41 = CreateBinaryTreeNode();
  156. BinaryTreeNode* pNode42 = CreateBinaryTreeNode();
  157. BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
  158. BinaryTreeNode* pNode22 = CreateBinaryTreeNode();
  159. BinaryTreeNode* pNode11 = CreateBinaryTreeNode();
  160. BinaryTreeNode* pNode12 = CreateBinaryTreeNode();
  161.  
  162. ConnectTreeNodes(pNode5, pNode31, pNode32);
  163. ConnectTreeNodes(pNode31, pNode41, nullptr);
  164. ConnectTreeNodes(pNode32, nullptr, pNode42);
  165. ConnectTreeNodes(pNode41, pNode6, nullptr);
  166. ConnectTreeNodes(pNode42, nullptr, pNode22);
  167. ConnectTreeNodes(pNode6, pNode11, nullptr);
  168. ConnectTreeNodes(pNode22, nullptr, pNode12);
  169.  
  170. Test("Test5", pNode5, false);
  171.  
  172. DestroyTree(pNode5);
  173. }
  174.  
  175. // 5
  176. // / \
  177. // 3 3
  178. // / \
  179. // 4 4
  180. // / \
  181. // 2 2
  182. // \
  183. // 1
  184. void Test6()
  185. {
  186. BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
  187. BinaryTreeNode* pNode31 = CreateBinaryTreeNode();
  188. BinaryTreeNode* pNode32 = CreateBinaryTreeNode();
  189. BinaryTreeNode* pNode41 = CreateBinaryTreeNode();
  190. BinaryTreeNode* pNode42 = CreateBinaryTreeNode();
  191. BinaryTreeNode* pNode21 = CreateBinaryTreeNode();
  192. BinaryTreeNode* pNode22 = CreateBinaryTreeNode();
  193. BinaryTreeNode* pNode12 = CreateBinaryTreeNode();
  194.  
  195. ConnectTreeNodes(pNode5, pNode31, pNode32);
  196. ConnectTreeNodes(pNode31, pNode41, nullptr);
  197. ConnectTreeNodes(pNode32, nullptr, pNode42);
  198. ConnectTreeNodes(pNode41, pNode21, nullptr);
  199. ConnectTreeNodes(pNode42, nullptr, pNode22);
  200. ConnectTreeNodes(pNode21, nullptr, nullptr);
  201. ConnectTreeNodes(pNode22, nullptr, pNode12);
  202.  
  203. Test("Test6", pNode5, false);
  204.  
  205. DestroyTree(pNode5);
  206. }
  207.  
  208. // 只有一个结点
  209. void Test7()
  210. {
  211. BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
  212. Test("Test7", pNode1, true);
  213.  
  214. DestroyTree(pNode1);
  215. }
  216.  
  217. // 没有结点
  218. void Test8()
  219. {
  220. Test("Test8", nullptr, true);
  221. }
  222.  
  223. // 所有结点都有相同的值,树对称
  224. // 5
  225. // / \
  226. // 5 5
  227. // / \
  228. // 5 5
  229. // / \
  230. // 5 5
  231. void Test9()
  232. {
  233. BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
  234. BinaryTreeNode* pNode21 = CreateBinaryTreeNode();
  235. BinaryTreeNode* pNode22 = CreateBinaryTreeNode();
  236. BinaryTreeNode* pNode31 = CreateBinaryTreeNode();
  237. BinaryTreeNode* pNode32 = CreateBinaryTreeNode();
  238. BinaryTreeNode* pNode41 = CreateBinaryTreeNode();
  239. BinaryTreeNode* pNode42 = CreateBinaryTreeNode();
  240.  
  241. ConnectTreeNodes(pNode1, pNode21, pNode22);
  242. ConnectTreeNodes(pNode21, pNode31, nullptr);
  243. ConnectTreeNodes(pNode22, nullptr, pNode32);
  244. ConnectTreeNodes(pNode31, pNode41, nullptr);
  245. ConnectTreeNodes(pNode32, nullptr, pNode42);
  246. ConnectTreeNodes(pNode41, nullptr, nullptr);
  247. ConnectTreeNodes(pNode42, nullptr, nullptr);
  248.  
  249. Test("Test9", pNode1, true);
  250.  
  251. DestroyTree(pNode1);
  252. }
  253.  
  254. // 所有结点都有相同的值,树不对称
  255. // 5
  256. // / \
  257. // 5 5
  258. // / \
  259. // 5 5
  260. // / /
  261. // 5 5
  262. void Test10()
  263. {
  264. BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
  265. BinaryTreeNode* pNode21 = CreateBinaryTreeNode();
  266. BinaryTreeNode* pNode22 = CreateBinaryTreeNode();
  267. BinaryTreeNode* pNode31 = CreateBinaryTreeNode();
  268. BinaryTreeNode* pNode32 = CreateBinaryTreeNode();
  269. BinaryTreeNode* pNode41 = CreateBinaryTreeNode();
  270. BinaryTreeNode* pNode42 = CreateBinaryTreeNode();
  271.  
  272. ConnectTreeNodes(pNode1, pNode21, pNode22);
  273. ConnectTreeNodes(pNode21, pNode31, nullptr);
  274. ConnectTreeNodes(pNode22, nullptr, pNode32);
  275. ConnectTreeNodes(pNode31, pNode41, nullptr);
  276. ConnectTreeNodes(pNode32, pNode42, nullptr);
  277. ConnectTreeNodes(pNode41, nullptr, nullptr);
  278. ConnectTreeNodes(pNode42, nullptr, nullptr);
  279.  
  280. Test("Test10", pNode1, false);
  281.  
  282. DestroyTree(pNode1);
  283. }
  284.  
  285. void main(int argc, char* argv[])
  286. {
  287. Test1();
  288. Test2();
  289. Test3();
  290. Test4();
  291. Test5();
  292. Test6();
  293. Test7();
  294. Test8();
  295. Test9();
  296. Test10();
  297. system("pause");
  298. }

《剑指offer》第二十八题(对称的二叉树)的更多相关文章

  1. 剑指offer五十八之对称的二叉树

    一.题目 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的.二.思路 递归做,详见代码 三.代码 /* public class TreeN ...

  2. 剑指Offer(十八):二叉树的镜像

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

  3. 【剑指offer】面试题 28. 对称的二叉树

    面试题 28. 对称的二叉树 题目描述 题目:请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. 解答过程 给定一个二叉树,检查它是否是镜像 ...

  4. 《剑指offer》第八题(重要!查找二叉树的中序遍历的下一个结点)

    文件一:main.cpp // 面试题:二叉树的下一个结点 // 题目:给定一棵二叉树和其中的一个结点,如何找出中序遍历顺序的下一个结点? // 树中的结点除了有两个分别指向左右子结点的指针以外,还有 ...

  5. 剑指offer四十八之不用加减乘除做加法

    一.题目 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 二.思路 1. 采用位运算的方法,分三步: (1).两个数异或:相当于每一位相加,而不考虑进位 (2).两个数 ...

  6. 剑指offer二十八之数组中出现次数超过一半的数字

    一.题目 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...

  7. 剑指offer三十八之二叉树的深度

    一.题目 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 二.思路 递归,详见代码. 三.代码 public class So ...

  8. 剑指Offer(书):对称的二叉树

    题目:请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. boolean isSymmetrical(TreeNode pRoot) { r ...

  9. 剑指offer第二版面试题6:重建二叉树(JAVA版)

    题目:输入某二叉树的前序遍历和中序遍历的结果,请重新构造出该二叉树.假设输入的前序遍历和中序遍历的结果中不包含重复的数字.例如输入的前序遍历序列为{1,2,4,7,3,5,6,8}和中序遍历为{4,7 ...

  10. 【剑指Offer】面试题28. 对称的二叉树

    题目 请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和它的镜像一样,那么它是对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的.     1    / \   2   2 ...

随机推荐

  1. 2:2 strus2的配置文件

    strus2 的xml配置文件主要负责Action的管理,常放在WEB-INF/classes目录下,被自动加载 在strus-core jar包下找dtd文件,里面有xml的头信息.也有contan ...

  2. Linux 安装gcc、gcc-c++编译器

    安装环境 Red Hat Enterprise Linux Server release 7.3 (Maipo) 方式一:yum安装 使用ISO制作yum源:Linux 使用系统ISO制作yum源 y ...

  3. Solr安装中文分词器IK

    安装环境 jdk1.7 solr-4.10.3.tgz KAnalyzer2012FF_u1.jar tomcat7 VM虚拟机redhat6.5-x64:192.168.1.201 Xshell4 ...

  4. Linux服务器配置---配置telnet

    配置telnet      通过配置文件,我们可以设置telnet的连接时间.连接数.连接ip等,实现更加安全的连接 1.设置连接时间,参数“access_times” [root@localhost ...

  5. Vue源码解析之数组变异

    力有不逮的对象 众所周知,在 Vue 中,直接修改对象属性的值无法触发响应式.当你直接修改了对象属性的值,你会发现,只有数据改了,但是页面内容并没有改变. 这是什么原因? 原因在于: Vue 的响应式 ...

  6. Python之路----内置函数补充与匿名函数

    内置函数补充:reversed()保留原列表,返回一个反向的迭代器 l = [1,2,3,4,5] l.reverse() print(l) l = [1,2,3,4,5] l2 = reversed ...

  7. 轻量级文本标记语言-Markdown

    Markdown简介 接触过github的都知道,在发布项目的时候可以建立一个说明文件README.md,这个md文件就是Markdown文本编辑语言的文件. Markdown 是一种轻量级标记语言, ...

  8. HttpClient4.5简单使用

    一.HttpClient简介 HttpClient是一个客户端的HTTP通信实现库,它不是一个浏览器.关于HTTP协议,可以搜索相关的资料.它设计的目的是发送与接收HTTP报文.它不会执行嵌入在页面中 ...

  9. Go第十篇之反射

    反射是指在程序运行期对程序本身进行访问和修改的能力.程序在编译时,变量被转换为内存地址,变量名不会被编译器写入到可执行部分.在运行程序时,程序无法获取自身的信息. 支持反射的语言可以在程序编译期将变量 ...

  10. IntelliJ IDEA 设置代码提示或自动补全的快捷键(Alt+/)

    点击 文件菜单(File) –> 点击 设置(Settings… Ctrl+Alt+S), –> 打开设置对话框.在左侧的导航框中点击 KeyMap. 接着在右边的树型框中选择 Main ...