《剑指offer》第二十八题(对称的二叉树)
- // 面试题28:对称的二叉树
- // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和
- // 它的镜像一样,那么它是对称的。
- #include <iostream>
- #include "BinaryTree.h"
- bool isSymmetrical(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2);
- bool isSymmetrical(BinaryTreeNode* pRoot)
- {
- return isSymmetrical(pRoot, pRoot);
- }
- bool isSymmetrical(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2)
- {
- if (pRoot1 == nullptr && pRoot2 == nullptr)//当二者都为空,true
- return true;
- if (pRoot1 == nullptr || pRoot2 == nullptr)//只有一个为空,flase
- return false;
- if (pRoot1->m_nValue != pRoot2->m_nValue)//当二者值不等,flase
- return false;
- return isSymmetrical(pRoot1->m_pLeft, pRoot2->m_pRight)
- && isSymmetrical(pRoot1->m_pRight, pRoot2->m_pLeft);//看看你镜像和我是不是一样~
- }//注意这里有个关键是,要测试是否都是空的,详见test9和10
- // ====================测试代码====================
- void Test(const char* testName, BinaryTreeNode* pRoot, bool expected)
- {
- if (testName != nullptr)
- printf("%s begins: ", testName);
- if (isSymmetrical(pRoot) == expected)
- printf("Passed.\n");
- else
- printf("FAILED.\n");
- }
- // 8
- // 6 6
- // 5 7 7 5
- void Test1()
- {
- BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode61 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode62 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode51 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode71 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode72 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode52 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNode8, pNode61, pNode62);
- ConnectTreeNodes(pNode61, pNode51, pNode71);
- ConnectTreeNodes(pNode62, pNode72, pNode52);
- Test("Test1", pNode8, true);
- DestroyTree(pNode8);
- }
- // 8
- // 6 9
- // 5 7 7 5
- void Test2()
- {
- BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode61 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode9 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode51 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode71 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode72 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode52 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNode8, pNode61, pNode9);
- ConnectTreeNodes(pNode61, pNode51, pNode71);
- ConnectTreeNodes(pNode9, pNode72, pNode52);
- Test("Test2", pNode8, false);
- DestroyTree(pNode8);
- }
- // 8
- // 6 6
- // 5 7 7
- void Test3()
- {
- BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode61 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode62 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode51 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode71 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode72 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNode8, pNode61, pNode62);
- ConnectTreeNodes(pNode61, pNode51, pNode71);
- ConnectTreeNodes(pNode62, pNode72, nullptr);
- Test("Test3", pNode8, false);
- DestroyTree(pNode8);
- }
- // 5
- // / \
- // 3 3
- // / \
- // 4 4
- // / \
- // 2 2
- // / \
- // 1 1
- void Test4()
- {
- BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode31 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode32 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode41 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode42 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode21 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode22 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode11 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode12 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNode5, pNode31, pNode32);
- ConnectTreeNodes(pNode31, pNode41, nullptr);
- ConnectTreeNodes(pNode32, nullptr, pNode42);
- ConnectTreeNodes(pNode41, pNode21, nullptr);
- ConnectTreeNodes(pNode42, nullptr, pNode22);
- ConnectTreeNodes(pNode21, pNode11, nullptr);
- ConnectTreeNodes(pNode22, nullptr, pNode12);
- Test("Test4", pNode5, true);
- DestroyTree(pNode5);
- }
- // 5
- // / \
- // 3 3
- // / \
- // 4 4
- // / \
- // 6 2
- // / \
- // 1 1
- void Test5()
- {
- BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode31 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode32 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode41 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode42 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode22 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode11 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode12 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNode5, pNode31, pNode32);
- ConnectTreeNodes(pNode31, pNode41, nullptr);
- ConnectTreeNodes(pNode32, nullptr, pNode42);
- ConnectTreeNodes(pNode41, pNode6, nullptr);
- ConnectTreeNodes(pNode42, nullptr, pNode22);
- ConnectTreeNodes(pNode6, pNode11, nullptr);
- ConnectTreeNodes(pNode22, nullptr, pNode12);
- Test("Test5", pNode5, false);
- DestroyTree(pNode5);
- }
- // 5
- // / \
- // 3 3
- // / \
- // 4 4
- // / \
- // 2 2
- // \
- // 1
- void Test6()
- {
- BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode31 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode32 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode41 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode42 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode21 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode22 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode12 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNode5, pNode31, pNode32);
- ConnectTreeNodes(pNode31, pNode41, nullptr);
- ConnectTreeNodes(pNode32, nullptr, pNode42);
- ConnectTreeNodes(pNode41, pNode21, nullptr);
- ConnectTreeNodes(pNode42, nullptr, pNode22);
- ConnectTreeNodes(pNode21, nullptr, nullptr);
- ConnectTreeNodes(pNode22, nullptr, pNode12);
- Test("Test6", pNode5, false);
- DestroyTree(pNode5);
- }
- // 只有一个结点
- void Test7()
- {
- BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
- Test("Test7", pNode1, true);
- DestroyTree(pNode1);
- }
- // 没有结点
- void Test8()
- {
- Test("Test8", nullptr, true);
- }
- // 所有结点都有相同的值,树对称
- // 5
- // / \
- // 5 5
- // / \
- // 5 5
- // / \
- // 5 5
- void Test9()
- {
- BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode21 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode22 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode31 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode32 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode41 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode42 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNode1, pNode21, pNode22);
- ConnectTreeNodes(pNode21, pNode31, nullptr);
- ConnectTreeNodes(pNode22, nullptr, pNode32);
- ConnectTreeNodes(pNode31, pNode41, nullptr);
- ConnectTreeNodes(pNode32, nullptr, pNode42);
- ConnectTreeNodes(pNode41, nullptr, nullptr);
- ConnectTreeNodes(pNode42, nullptr, nullptr);
- Test("Test9", pNode1, true);
- DestroyTree(pNode1);
- }
- // 所有结点都有相同的值,树不对称
- // 5
- // / \
- // 5 5
- // / \
- // 5 5
- // / /
- // 5 5
- void Test10()
- {
- BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode21 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode22 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode31 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode32 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode41 = CreateBinaryTreeNode();
- BinaryTreeNode* pNode42 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNode1, pNode21, pNode22);
- ConnectTreeNodes(pNode21, pNode31, nullptr);
- ConnectTreeNodes(pNode22, nullptr, pNode32);
- ConnectTreeNodes(pNode31, pNode41, nullptr);
- ConnectTreeNodes(pNode32, pNode42, nullptr);
- ConnectTreeNodes(pNode41, nullptr, nullptr);
- ConnectTreeNodes(pNode42, nullptr, nullptr);
- Test("Test10", pNode1, false);
- DestroyTree(pNode1);
- }
- void main(int argc, char* argv[])
- {
- Test1();
- Test2();
- Test3();
- Test4();
- Test5();
- Test6();
- Test7();
- Test8();
- Test9();
- Test10();
- system("pause");
- }
《剑指offer》第二十八题(对称的二叉树)的更多相关文章
- 剑指offer五十八之对称的二叉树
一.题目 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的.二.思路 递归做,详见代码 三.代码 /* public class TreeN ...
- 剑指Offer(十八):二叉树的镜像
剑指Offer(十八):二叉树的镜像 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/baidu ...
- 【剑指offer】面试题 28. 对称的二叉树
面试题 28. 对称的二叉树 题目描述 题目:请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. 解答过程 给定一个二叉树,检查它是否是镜像 ...
- 《剑指offer》第八题(重要!查找二叉树的中序遍历的下一个结点)
文件一:main.cpp // 面试题:二叉树的下一个结点 // 题目:给定一棵二叉树和其中的一个结点,如何找出中序遍历顺序的下一个结点? // 树中的结点除了有两个分别指向左右子结点的指针以外,还有 ...
- 剑指offer四十八之不用加减乘除做加法
一.题目 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 二.思路 1. 采用位运算的方法,分三步: (1).两个数异或:相当于每一位相加,而不考虑进位 (2).两个数 ...
- 剑指offer二十八之数组中出现次数超过一半的数字
一.题目 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...
- 剑指offer三十八之二叉树的深度
一.题目 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 二.思路 递归,详见代码. 三.代码 public class So ...
- 剑指Offer(书):对称的二叉树
题目:请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. boolean isSymmetrical(TreeNode pRoot) { r ...
- 剑指offer第二版面试题6:重建二叉树(JAVA版)
题目:输入某二叉树的前序遍历和中序遍历的结果,请重新构造出该二叉树.假设输入的前序遍历和中序遍历的结果中不包含重复的数字.例如输入的前序遍历序列为{1,2,4,7,3,5,6,8}和中序遍历为{4,7 ...
- 【剑指Offer】面试题28. 对称的二叉树
题目 请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和它的镜像一样,那么它是对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 ...
随机推荐
- 2:2 strus2的配置文件
strus2 的xml配置文件主要负责Action的管理,常放在WEB-INF/classes目录下,被自动加载 在strus-core jar包下找dtd文件,里面有xml的头信息.也有contan ...
- Linux 安装gcc、gcc-c++编译器
安装环境 Red Hat Enterprise Linux Server release 7.3 (Maipo) 方式一:yum安装 使用ISO制作yum源:Linux 使用系统ISO制作yum源 y ...
- Solr安装中文分词器IK
安装环境 jdk1.7 solr-4.10.3.tgz KAnalyzer2012FF_u1.jar tomcat7 VM虚拟机redhat6.5-x64:192.168.1.201 Xshell4 ...
- Linux服务器配置---配置telnet
配置telnet 通过配置文件,我们可以设置telnet的连接时间.连接数.连接ip等,实现更加安全的连接 1.设置连接时间,参数“access_times” [root@localhost ...
- Vue源码解析之数组变异
力有不逮的对象 众所周知,在 Vue 中,直接修改对象属性的值无法触发响应式.当你直接修改了对象属性的值,你会发现,只有数据改了,但是页面内容并没有改变. 这是什么原因? 原因在于: Vue 的响应式 ...
- Python之路----内置函数补充与匿名函数
内置函数补充:reversed()保留原列表,返回一个反向的迭代器 l = [1,2,3,4,5] l.reverse() print(l) l = [1,2,3,4,5] l2 = reversed ...
- 轻量级文本标记语言-Markdown
Markdown简介 接触过github的都知道,在发布项目的时候可以建立一个说明文件README.md,这个md文件就是Markdown文本编辑语言的文件. Markdown 是一种轻量级标记语言, ...
- HttpClient4.5简单使用
一.HttpClient简介 HttpClient是一个客户端的HTTP通信实现库,它不是一个浏览器.关于HTTP协议,可以搜索相关的资料.它设计的目的是发送与接收HTTP报文.它不会执行嵌入在页面中 ...
- Go第十篇之反射
反射是指在程序运行期对程序本身进行访问和修改的能力.程序在编译时,变量被转换为内存地址,变量名不会被编译器写入到可执行部分.在运行程序时,程序无法获取自身的信息. 支持反射的语言可以在程序编译期将变量 ...
- IntelliJ IDEA 设置代码提示或自动补全的快捷键(Alt+/)
点击 文件菜单(File) –> 点击 设置(Settings… Ctrl+Alt+S), –> 打开设置对话框.在左侧的导航框中点击 KeyMap. 接着在右边的树型框中选择 Main ...