剑指offer——面试题27:二叉树的镜像
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);
}
}
函数循环
#include"BinaryTree.h" // ====================测试代码====================
// 测试完全二叉树:除了叶子节点,其他节点都有两个子节点
// 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(); return ;
}
测试代码
剑指offer——面试题27:二叉树的镜像的更多相关文章
- 剑指Offer:面试题19——二叉树的镜像(java实现)
问题描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树结点定义为: public class TreeNode { int val = 0; TreeNode left = null; Tr ...
- 剑指offer面试题19 二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- 剑指Offer - 九度1521 - 二叉树的镜像
剑指Offer - 九度1521 - 二叉树的镜像2013-11-30 23:32 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入 ...
- 剑指offer:对称的二叉树(镜像,递归,非递归DFS栈+BFS队列)
1. 题目描述 /** 请实现一个函数,用来判断一颗二叉树是不是对称的. 注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的 */ 2. 递归 思路: /** 1.只要pRoot.left和 ...
- 剑指offer(18)二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- 剑指offer十八之二叉树的镜像
一.题目 操作给定的二叉树,将其变换为源二叉树的镜像.二叉树的镜像定义: 源二叉树 : 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树: 8 / \ 10 6 / \ ...
- 【剑指Offer】18、二叉树的镜像
题目描述: 操作给定的二叉树,将其变换为原二叉树的镜像. 解题思路: 求一棵树的镜像的过程:先前序遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子结点.当交换完所有的非 ...
- 剑指Offer:面试题25——二叉树中和为某一值的路径(java实现)
问题描述: 输入一棵二叉树和一个整数,打印出二叉树中结点指的和为输入整数的所有路径.从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.二叉树结点的定义如下: public class Tree ...
- 剑指offer面试题27:二叉搜索树与双向链表
题目:输入一颗二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的节点,只能调整树中节点指针的指向. 由于二叉搜索树是有序的,左子结点的值小于根节点的值,右子结点的值大于根节点的值 ...
随机推荐
- C++ 数据封装和抽象
C++ 数据抽象 数据抽象是指,只向外界提供关键信息,并隐藏其后台的实现细节,即只表现必要的信息而不呈现细节. 数据抽象是一种依赖于接口和实现分离的编程(设计)技术. 让我们举一个现实生活中的真实例子 ...
- Linq编译带来的诡异错误
今天遇到一个很诡异的问题,初步猜测是Linq编译以及编译器自动优化带来的原因,对IL不是很熟悉,所以懒得去追了. 贴个代码出来,希望能抛砖引玉,得到正解. 注意到我用原始的foreach语句代替了li ...
- Window vista 以上制作自定义证书并为端口配置ssl
此处的关键在于证书需要分两步,不然在配置ssl时总会有错误.也许makecert命令也会有些玄机,但是管他呢,请按以下步骤和命令配置,几分钟就可成功: 证书制作: 1, 在开始/所有程序(或其他地方 ...
- 百度地图api描绘车辆历史轨迹图
最近公司在做项目需需求:车辆定位后在地图显示历史轨迹的功能 一开始使用了google的地图api,但是发现会一直关闭,索性支持下国产,使用了百度地图api search方法把两个点连接成线后,会出现起 ...
- NETSH.EXE操作SSL
NETSH.EXE操作SSL 程序位置:c:\windows\syswow64\netsh.exe 查看当前端口配置 netsh http show sslcert 将 SSL 证书绑定至端口号 ne ...
- Android-有序广播
在之前的博客,Android-广播概念,中介绍了(广播和广播接收者)可以组件与组件之间进行通讯,有两种类型的广播(无序广播 和 有序广播),这篇博客就来讲解有序广播的代码实现: 有序广播:接收者 可以 ...
- 采购文件中 RFI、RFQ、RFP、IFB的区别
[PMBOK的描述] 采购文件用于征求潜在卖方的建议书.如果主要依据价格来选择卖方(如购买商业或标准产品时),通常就使用标书.投标或报价等术语.如果主要依据其他考虑(如技术能力或技术方法)来选择卖方, ...
- nginx 配置中的if判断
正则表达式匹配: ==:等值比较; ~:与指定正则表达式模式匹配时返回“真”,判断匹配与否时区分字符大小写: ~*:与指定正则表达式模式匹配时返回“真”,判断匹配与否时不区分字 ...
- Lock的await/singal 和 Object的wait/notify 的区别
在使用Lock之前,我们都使用Object 的wait和notify实现同步的.举例来说,一个producer和consumer,consumer发现没有东西了,等待,produer生成东西了,唤醒. ...
- 2018 ACM-ICPC 亚洲区域赛北京现场赛 I题 Palindromes
做法:打表找规律 大数是过不了这个题的(但可以用来打表) 先找k的前缀,前缀对应边缘数字是哪个 如果第0位是2-9 对应奇数长度的1-8 第0位为1时,第1位为0时对应奇数长度的9,为1-9时对应偶数 ...