《剑指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 ...
随机推荐
- Log4net 日志传到 graylog监控
graylog是java的一个日志监控插件.存储用的是mongoDB,效率还是挺高的.不过嘛,文档太少了,安装和配置都很不容易. 官网:http://www.graylog.org/ 在graylog ...
- matlab 绘制条状图形
clear,clc;A=zeros(1080,1920,3);A(:,1:384,:)=0;A(:,385:768,:)=10;A(:,769:1152,:)=20;A(:,1153:1536,:)= ...
- css 文本超出容器长度后自动省略的方法!
我们在给用户显示文本内容的时候,往往需要避免文本内容超出容器宽度,防止换行溢出,小弟在网上找了下发现网上的实现仅仅只是实现了用 ...省略了的功能! 而并没有获取光标提示的功能,所有小弟就结合网上的代 ...
- 第一篇 Python图片处理模块PIL(pillow)
本篇包含:一.Image类的属性:1.Format 2.Mode 3.Size 4.Palette 5.Info 二.类的函数:1.New ...
- MyBatis学习笔记(三)——优化MyBatis配置文件中的配置
转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4264301.html 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的 ...
- linux常用命令:whereis 命令
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis查找的速度非 ...
- linux常用命令:grep 命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- linux下删除大量文件提示参数过长解决办法
linux下删除大量文件提示参数过长解决办法:在当前目录下rm -rf * 在linux中删除大量文件时,直接用rm会出现:-bash: /bin/rm: 参数列表过长的错误. 这时可以用find命令 ...
- 硬件中断和DPC一直占40-52%左右 解决方法
硬件中断和DPC一直占40-52%左右,突然感觉电脑变慢 重启后竟然启动不了了,冷却一段时间后才能进去,温度检测cpu,硬盘都超标了! 用Process Explorer检测硬件中断和DPC 占cpu ...
- Java eclipse下 Ant build.xml实例详解 附完整项目源码
在有eclipse集成环境下ant其实不是很重要,但有些项目需要用到,另外通过eclipse来学习和理解ant是个很好的途径,所以写他demo总结下要点,希望能够帮到大家. 一.本人测试环境eclip ...