剑指offer18:操作给定的二叉树,将其变换为源二叉树的镜像。
1 题目描述
2 输入描述:
二叉树的镜像定义:源二叉树
8
/ \
6 10
/ \ / \
5 7 9 11
镜像二叉树
8
/ \
10 6
/ \ / \
11 9 7 5
3 思路和方法
(1)递归思想,先交换根节点的左右子树的位置,然后向下递归,把左右子树的根节点作为下次循环的根节点。
(2)非递归:所以我们可以采用前序遍历的方法进行操作,每当遇到一个结点的时候,首先判断其是否有左右孩子(有其中之一即可),如果有的话,就交换其左右孩子,然后继续遍历其左右子树,只要不为空就继续交换其左右孩子节点(在交换具有孩子结点的结点的时候,其孩子结点也一并被交换了)。
4. C++核心代码
4.1 循环实现二叉树的镜像,利用栈的“后进先出”特性打印
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
void Mirror(TreeNode *root) {
if (root == NULL)
return; stack<TreeNode*> stackTreeNode;
stackTreeNode.push(root); while (stackTreeNode.size() > )
{
TreeNode *parent = stackTreeNode.top();
stackTreeNode.pop(); TreeNode *Temp = parent->left;
parent->left = parent->right;
parent->right = Temp; if (parent->left)
stackTreeNode.push(parent->left);
if (parent->right)
stackTreeNode.push(parent->right);
}
}
};
4.2 递归实现二叉树的镜像,按照先序遍历,如果遇到空的节点或者叶子节点就返回,否则交换两个子树后再改变左右子树
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
void Mirror(TreeNode *pRroot) {
if (pRroot == NULL || (pRroot->left == NULL && pRroot->right == NULL))
return;
TreeNode * tmp = pRroot->left;
pRroot->left = pRroot->right;
pRroot->right = tmp; if (pRroot->left)
Mirror(pRroot->left);
if (pRroot->right)
Mirror(pRroot->right);
}
};
5. 完整代码
#include<iostream>
#include<stack>
using namespace std; struct BinaryTreeNode
{
int data;
BinaryTreeNode* leftchild;
BinaryTreeNode* rightchild; BinaryTreeNode(int t)
{
data = t;
leftchild = NULL;
rightchild = NULL;
}
}; void PreorderTravel(BinaryTreeNode* root)
{
if (root == NULL)
{
return;
}
cout << root->data << " ";
PreorderTravel(root->leftchild);
PreorderTravel(root->rightchild);
} //递归实现二叉树的镜像,按照先序遍历,如果遇到空的节点或者叶子节点就返回,否则交换两个子树后再改变左右子树
void MirrorBinaryTree1(BinaryTreeNode* root)
{
if (root == NULL || (root->leftchild == NULL && root->rightchild == NULL))
{
return;
} BinaryTreeNode * tmp = root->leftchild;
root->leftchild = root->rightchild;
root->rightchild = tmp; if (root->leftchild)
{
MirrorBinaryTree1(root->leftchild);
}
if (root->rightchild)
{
MirrorBinaryTree1(root->rightchild);
} } //循环实现二叉树的镜像,利用栈的“后进先出”特性打印
void MirrorBinaryTree2(BinaryTreeNode* root)
{
if (root == NULL)
{
return;
} stack<BinaryTreeNode*> stackTreeNode;
stackTreeNode.push(root); while (stackTreeNode.size() > )
{
BinaryTreeNode *parent = stackTreeNode.top();
stackTreeNode.pop(); BinaryTreeNode *Temp = parent->leftchild;
parent->leftchild = parent->rightchild;
parent->rightchild = Temp; if (parent->leftchild)
{
stackTreeNode.push(parent->leftchild);
} if (parent->rightchild)
{
stackTreeNode.push(parent->rightchild);
} }
} // ====================测试代码==================== // 测试完全二叉树:除了叶子节点,其他节点都有两个子节点
// 8
// 6 10
// 5 7 9 11 BinaryTreeNode* root;
void Test1()
{
root = new BinaryTreeNode();
root->leftchild = new BinaryTreeNode();
root->rightchild = new BinaryTreeNode();
BinaryTreeNode* tmp = root->leftchild;
tmp->leftchild = new BinaryTreeNode();
tmp->rightchild = new BinaryTreeNode();
tmp = root->rightchild;
tmp->leftchild = new BinaryTreeNode();
tmp->rightchild = new BinaryTreeNode(); cout << "Test1:测试完全二叉树,除了叶子节点,其他节点都有两个子节点" << endl;
cout << "原二叉树的先序遍历" << endl;
PreorderTravel(root);
cout << endl; MirrorBinaryTree1(root);
cout << "二叉树镜像后的先序遍历" << endl;
PreorderTravel(root);
cout << endl; /*MirrorBinaryTree2(root);
cout << "二叉树镜像后的先序遍历" << endl;
PreorderTravel(root);
cout << endl;*/
} // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个左子结点
// 8
// 7
// 6
// 5
//
void Test2()
{
root = new BinaryTreeNode();
root->leftchild = new BinaryTreeNode();
root->rightchild = NULL; BinaryTreeNode* tmp = root->leftchild;
tmp->leftchild = new BinaryTreeNode();
tmp->rightchild = NULL; tmp = tmp->leftchild;
tmp->leftchild = new BinaryTreeNode();
tmp->rightchild = NULL; tmp = tmp->leftchild;
tmp->leftchild = new BinaryTreeNode();
tmp->rightchild = NULL; cout << "Test2: 测试二叉树,出叶子结点之外,左右的结点都有且只有一个左子结点" << endl;
cout << "原二叉树的先序遍历" << endl;
PreorderTravel(root);
cout << endl; MirrorBinaryTree1(root);
cout << "二叉树镜像后的先序遍历" << endl;
PreorderTravel(root);
cout << endl; /*MirrorBinaryTree2(root);
cout << "二叉树镜像后的先序遍历" << endl;
PreorderTravel(root);
cout << endl;*/
} // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个右子结点
// 8
// 7
// 6
// 5
// 4
void Test3()
{
root = new BinaryTreeNode();
root->leftchild = NULL;
root->rightchild = new BinaryTreeNode(); BinaryTreeNode* tmp = root->rightchild;
tmp->leftchild = NULL;
tmp->rightchild = new BinaryTreeNode(); tmp = tmp->rightchild;
tmp->leftchild = NULL;
tmp->rightchild = new BinaryTreeNode(); tmp = tmp->rightchild;
tmp->leftchild = NULL;
tmp->rightchild = new BinaryTreeNode(); cout << "Test3:测试二叉树出叶子结点之外,左右的结点都有且只有一个右子结点" << endl;
cout << "原二叉树的先序遍历" << endl;
PreorderTravel(root);
cout << endl; MirrorBinaryTree1(root);
cout << "二叉树镜像后的先序遍历" << endl;
PreorderTravel(root);
cout << endl; /*MirrorBinaryTree2(root);
cout << "二叉树镜像后的先序遍历" << endl;
PreorderTravel(root);
cout << endl;*/
} // 测试空二叉树:根结点为空指针
void Test4()
{
root = NULL; cout << "Test4:测试空二叉树,根结点为空指针" << endl;
cout << "原二叉树的先序遍历" << endl;
PreorderTravel(root);
cout << endl; MirrorBinaryTree1(root);
cout << "二叉树镜像后的先序遍历" << endl;
PreorderTravel(root);
cout << endl; /*MirrorBinaryTree2(root);
cout << "二叉树镜像后的先序遍历" << endl;
PreorderTravel(root);
cout << endl;*/
} // 测试只有一个结点的二叉树
void Test5()
{
root = new BinaryTreeNode();
root->leftchild = NULL;
root->rightchild = NULL; cout << "Test5:测试只有一个结点8的二叉树" << endl;
cout << "原二叉树的先序遍历" << endl;
PreorderTravel(root);
cout << endl; MirrorBinaryTree1(root);
cout << "二叉树镜像后的先序遍历" << endl;
PreorderTravel(root);
cout << endl; /*MirrorBinaryTree2(root);
cout << "二叉树镜像后的先序遍历" << endl;
PreorderTravel(root);
cout << endl;*/
} int main()
{
Test1();
Test2();
Test3();
Test4();
Test5(); system("pause");
return ;
}
参考资料
https://blog.csdn.net/yanxiaolx/article/details/52019871
剑指offer18:操作给定的二叉树,将其变换为源二叉树的镜像。的更多相关文章
- 剑指Offer-18.二叉树的镜像(C++/Java)
题目: 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ ...
- 二叉树的镜像(剑指offer-18)
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 解析 先前序遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子节点, 当交换完所有的非叶子结点的左右子结点之后,就得到了树的镜像 ...
- C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解
剑指offer 面试题23:从上往下打印二叉树 参与人数:4853 时间限制:1秒 空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...
- 【javascript】操作给定的二叉树,将其变换为源二叉树的镜像。
操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 ...
- 剑指Offer的学习笔记(C#篇)-- 平衡二叉树(二叉树后序遍历递归详解版)
题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 一 . 题目分析 首先要理解一个概念:什么是平衡二叉树,如果某二叉树中任意的左右子树深度相差不超过1,那么他就是一颗平衡二叉树.如下图: 所以 ...
- 剑指OFFER18 判断一个二叉树的子树
public class a18_IsSubTree { public static boolean hasSubTree(TreeNode treeRoot1, TreeNode treeRoot2 ...
- 剑指Offer面试题:21.从上到下打印二叉树
一.题目:从上到下打印二叉树 题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印.例如输入下图中的二叉树,则依次打印出8.6.10.5.7.9.11. 二叉树节点的定义如下,采用 ...
- 剑指offer面试题23:从上到下打印二叉树(树的层序遍历)
题目:从上往下打印出二叉树的每个节点,同一层的结点按照从左往右的顺序打印. 解题思路:二叉树的层序遍历,在打印一个节点的时候,要把他的子节点保存起来打印第一层要把第二层的节点保存起来, 打印第二层要把 ...
- 剑指offer-字符的所有组合,复制复杂链表,二叉树中和为某一值的路径
字符的所有组合 描述: 输入一个字符串,求这个字符串中的字符的所有组合.如:"abc",组合为"a" "b" c" "a ...
随机推荐
- Luogu5298 [PKUWC2018]Minimax
太久没写博客了,过来水一发. 题目链接:洛谷 首先我们想到,考虑每个叶节点的权值为根节点权值的概率.首先要将叶节点权值离散化. 假设现在是$x$节点,令$f_i,g_i$分别表示左/右节点的权值$=i ...
- 以字符串为例,谈谈Python到底要学到什么程度
古语云:慈不掌兵,义不为商:离商业越近,离人性越远:我们在自学数据科学时,一定会辅助一些书籍或者视频来学习,怎么学习,选择哪些资料来学习?这时,我们都要理解好第一句话,理解不好,浪费钱是次要,重要的是 ...
- Spring Cloud Eureka(六):Eureka Client 如何注册到Eureka Server
1.本节概要 根据前文我们对Eureka Server 有了一定的了解,本节我们主要学习Eureka Client 与 Eureka Server 如何通讯的及相关通信机制是什么,本文会弄清楚一下几个 ...
- python线程+队列(queue)
---恢复内容开始--- python的线程学习 用处 pocpiliang脚本的编写 函数式:调用 _thread 模块中的start_new_thread()函数来产生新线程.语法如下: _thr ...
- NOI2019 游记——一切都是最好的安排
有幸运有遗憾 一切都是最好的安排. Day-3 临近NOI了,机房都在狂奶某某同学进队稳了 HE省队垫底,THUSC面试都没进 作为一个有自知之明的人 也就指望着能拼进前100,至少也拿个银牌. 心态 ...
- Windows系统配置Redis密码
1.首先停止Redis服务,直接关掉Redis或者打开任务管理器查看有没有redis-server进程,有的话选中并结束任务. 2.打开配置文件redis.windows.conf和redis.win ...
- spring boot 原理解析一(spring boot 基础特征)
spring boot 提供了完整的介绍 文档:https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/html/documen ...
- kotlin 代码格式
与java 不同,代码顺序影响编译过程. { var a:Int = 1; init(){ a++; } //var a:Int = 1; 写在 init 之后不可用. }
- Fegin的使用总结
/** * * @Description: 修改microservicecloud-api工程,根据已经有的DeptClientService接口 新建,MICROSERVICECLOUD-DEPT指 ...
- 网络编程三要素之IP
用来标示我们计算机在互联网上的唯一性 每个设备在网络中的唯一标识 每台网络终端在网络中都有一个独立的地址,我们在网络中传输数据就是使用这个地址. ipconfig:查看本机IP192.168.12.4 ...