《剑指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 ...
随机推荐
- java多态性方法的重写Overriding和重载Overloading详解
java多态性方法的重写Overriding和重载Overloading详解 方法的重写Overriding和重载Overloading是Java多态性的不同表现.重写Overriding是父类与子类 ...
- GridView 点滴
绑定数据时.在后台给GridView添加事件 protected void grd_RowDataBound(object sender, GridViewRowEventArgs e) { //当前 ...
- Django初级手册1-项目和应用的创建与简单的数据库操作
创建项目 django-admin.py startproject mysite 1. 目录结构 mysite/ #项目的名称 manage.py #可通过命令和项目进行交互的文件 mysite/ # ...
- 015-awk
1.awk的处理方式: 一次处理一行. 对每行可以进行切片处理,针对字段处理.2.awk的格式: awk BEGIN{循环之前初始化} [options] 'command' END{循环之后结尾} ...
- 001-linux中特殊权限
- javascript的Object对象的defineProperty和defineProperties
Object的属性 查看官网:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Obje ...
- uva1201 DAG 最小路径覆盖,转化为 二分图
大白例题P356 你在一座城市里负责一个大型活动的接待工作.你需要去送m个人从出发地到目的地,已知每个人的出发时间出发地点,和目的地点,你的任务是用尽量少的出租车送他们,使得每次出租车接客人,至少能提 ...
- # 20155327 2016-2017-3 《Java程序设计》第5周学习总结
20155327 2016-2017-3 <Java程序设计>第5周学习总结 教材学习内容总结 理解异常架构 粉红色的是受检查的异常(checked exceptions),其必须被 tr ...
- Maximum execution time of 30 seconds exceeded解决错误方法
Maximum execution time of 30 seconds exceeded解决错误方法Fatal error: Maximum execution time of 30 seconds ...
- 一个远程启动windows c++程序引发的技术决策现象
还是因为那个8点半前要启动近百套报盘程序的问题,差不多两周前表示自己会抽空给解决掉,一次性启动,直到昨天才差不多能够抽点时间出来开始想怎么解决的问题. 这个问题的复杂点在于除了启动exe外,还需要鼠标 ...