《剑指offer》第二十七题(二叉树的镜像)
// 面试题27:二叉树的镜像
// 题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像。 #include <iostream>
#include "BinaryTree.h"
#include <stack> void MirrorRecursively(BinaryTreeNode *pNode)//递归算法(自下而上)
{
if ((pNode == nullptr) || (pNode->m_pLeft == nullptr && pNode->m_pRight == nullptr))//鲁棒性
return; BinaryTreeNode *pTemp = pNode->m_pLeft;//交换
pNode->m_pLeft = pNode->m_pRight;
pNode->m_pRight = pTemp; if (pNode->m_pLeft)//遍历
MirrorRecursively(pNode->m_pLeft); if (pNode->m_pRight)
MirrorRecursively(pNode->m_pRight);
} void MirrorIteratively(BinaryTreeNode* pRoot)//迭代算法(自上而下)
{
if (pRoot == nullptr)
return; std::stack<BinaryTreeNode*> stackTreeNode;//建立一个栈
stackTreeNode.push(pRoot);//压入根节点 while (stackTreeNode.size() > )//当栈内有元素时
{
BinaryTreeNode *pNode = stackTreeNode.top();//取出栈顶元素
stackTreeNode.pop(); BinaryTreeNode *pTemp = pNode->m_pLeft;//交换该元素(即节点)的孩子
pNode->m_pLeft = pNode->m_pRight;
pNode->m_pRight = pTemp; if (pNode->m_pLeft)//如果节点孩子存在,把孩子压入堆栈
stackTreeNode.push(pNode->m_pLeft); if (pNode->m_pRight)
stackTreeNode.push(pNode->m_pRight);
}
} // ====================测试代码====================
// 测试完全二叉树:除了叶子节点,其他节点都有两个子节点
// 8
// 6 10
// 5 7 9 11
void Test1()
{
printf("=====Test1 starts:=====\n");
BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
BinaryTreeNode* pNode10 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode();
BinaryTreeNode* pNode9 = CreateBinaryTreeNode();
BinaryTreeNode* pNode11 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode8, pNode6, pNode10);
ConnectTreeNodes(pNode6, pNode5, pNode7);
ConnectTreeNodes(pNode10, pNode9, pNode11); PrintTree(pNode8); printf("=====Test1: MirrorRecursively=====\n");
MirrorRecursively(pNode8);
PrintTree(pNode8); printf("=====Test1: MirrorIteratively=====\n");
MirrorIteratively(pNode8);
PrintTree(pNode8); DestroyTree(pNode8);
} // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个左子结点
// 8
// 7
// 6
// 5
//
void Test2()
{
printf("=====Test2 starts:=====\n");
BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode();
BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode8, pNode7, nullptr);
ConnectTreeNodes(pNode7, pNode6, nullptr);
ConnectTreeNodes(pNode6, pNode5, nullptr);
ConnectTreeNodes(pNode5, pNode4, nullptr); PrintTree(pNode8); printf("=====Test2: MirrorRecursively=====\n");
MirrorRecursively(pNode8);
PrintTree(pNode8); printf("=====Test2: MirrorIteratively=====\n");
MirrorIteratively(pNode8);
PrintTree(pNode8); DestroyTree(pNode8);
} // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个右子结点
// 8
// 7
// 6
// 5
// 4
void Test3()
{
printf("=====Test3 starts:=====\n");
BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode();
BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode8, nullptr, pNode7);
ConnectTreeNodes(pNode7, nullptr, pNode6);
ConnectTreeNodes(pNode6, nullptr, pNode5);
ConnectTreeNodes(pNode5, nullptr, pNode4); PrintTree(pNode8); printf("=====Test3: MirrorRecursively=====\n");
MirrorRecursively(pNode8);
PrintTree(pNode8); printf("=====Test3: MirrorIteratively=====\n");
MirrorIteratively(pNode8);
PrintTree(pNode8); DestroyTree(pNode8);
} // 测试空二叉树:根结点为空指针
void Test4()
{
printf("=====Test4 starts:=====\n");
BinaryTreeNode* pNode = nullptr; PrintTree(pNode); printf("=====Test4: MirrorRecursively=====\n");
MirrorRecursively(pNode);
PrintTree(pNode); printf("=====Test4: MirrorIteratively=====\n");
MirrorIteratively(pNode);
PrintTree(pNode);
} // 测试只有一个结点的二叉树
void Test5()
{
printf("=====Test5 starts:=====\n");
BinaryTreeNode* pNode8 = CreateBinaryTreeNode(); PrintTree(pNode8); printf("=====Test4: MirrorRecursively=====\n");
MirrorRecursively(pNode8);
PrintTree(pNode8); printf("=====Test4: MirrorIteratively=====\n");
MirrorIteratively(pNode8);
PrintTree(pNode8);
} int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
system("pause");
return ;
}
《剑指offer》第二十七题(二叉树的镜像)的更多相关文章
- 剑指Offer - 九度1521 - 二叉树的镜像
剑指Offer - 九度1521 - 二叉树的镜像2013-11-30 23:32 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入 ...
- 剑指offer:对称的二叉树(镜像,递归,非递归DFS栈+BFS队列)
1. 题目描述 /** 请实现一个函数,用来判断一颗二叉树是不是对称的. 注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的 */ 2. 递归 思路: /** 1.只要pRoot.left和 ...
- 剑指Offer:面试题19——二叉树的镜像(java实现)
问题描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树结点定义为: public class TreeNode { int val = 0; TreeNode left = null; Tr ...
- 剑指offer(18)二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- 剑指offer十八之二叉树的镜像
一.题目 操作给定的二叉树,将其变换为源二叉树的镜像.二叉树的镜像定义: 源二叉树 : 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树: 8 / \ 10 6 / \ ...
- 剑指offer面试题19 二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- 【剑指Offer】18、二叉树的镜像
题目描述: 操作给定的二叉树,将其变换为原二叉树的镜像. 解题思路: 求一棵树的镜像的过程:先前序遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子结点.当交换完所有的非 ...
- 剑指offer第二版-8.二叉树的下一个节点
描述:给定一棵二叉树和其中的一个节点,找出中序遍历序列的下一个节点.树中应定义指向左节点.右节点.父节点的三个变量. 思路: 1.如果输入的当前节点有右孩子,则它的下一个节点即为该右孩子为根节点的子树 ...
- 剑指offer五十七之二叉树的下一个结点
一.题目 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 二.思路 结合图,我们可发现分成两大类: 1.有右子树 ...
- 剑指Offer(十七):树的子结构
剑指Offer(十七):树的子结构 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/baidu_ ...
随机推荐
- boost--序列化库serialization
序列化可以把对象转化成一个字节流存储或者传输,在需要时再回复成与原始状态一致的等价对象.C++标准没有定义这个功能.boost.serialization以库的形式提供了这个功能,非常强大,可以序列化 ...
- js中sort()方法冒泡排序模拟
1.sort()方法概述 sort() 方法用于对数组的元素进行排序. 如果调用该方法时没有使用参数,将按字母顺序对数组中的元素进行排序,说得更精确点,是按照字符编码的顺序进行排序.要实现这一点, 首 ...
- Javascript-蔬菜运算价格
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 集合框架—常见的Set集合
list ArrayList 动态数组结构存储,遍历速度快,索引随机访问快,允许多空值 LinkedList 底层数据结构是链表,插入和删除速度快. Vector 数组结构存储,线程安全的,查找速度快 ...
- mov指令
一.规定 1.立即数不能作为目的操作数,如mov 110H,AX 2.立即数不能直接传给段寄存器,如mov DS,110H 2.两个操作数不能同时为段寄存器,如mov ES,DS 3.两个操作数不能同 ...
- 5Lambda表达式
C++11中的Lambda表达式用于定义并创建匿名的函数对象,以简化编程工作.首先看一下Lambda表达式的基本构成: [函数对象参数](操作符重载函数参数)mutable或exception -&g ...
- C/S模型之TCP群聊
说明:利用TCP协议和多线程实现群聊功能.一个服务器,多个客户端(同一个程序多次启动).客户端向服务端发送数据,由服务端进行转发到其他客户端. /服务端 // WSASever.cpp : 定义控制台 ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON ZoomImageFactor
zw版[转发·台湾nvp系列Delphi例程]HALCON ZoomImageFactor procedure TForm1.Button1Click(Sender: TObject);var ima ...
- Linux服务器---DansGuardian
DansGuardian DansGuardian可以限制客户端的访问,通过这个软件,我们可以限制哪些网站不可以访问.哪些内容不能下载. 1.下载DansGuardian,提供一个网址http://w ...
- JDBC报错记录
用JDBC连接oracle时 有如下问题: 问题一.java.lang.ClassNotFoundException: oracle.jdbc.driver.oracledriver 解决: 可以在环 ...