/*************************************************************************
> File Name: 17_MirrorOfBinaryTree.cpp
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: 2016年08月30日 星期二 17时10分03秒
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <bits/stdc++.h> using namespace std; // 二叉树结构体
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
}; TreeNode* createTree()
{
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = ;
root->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->val = ;
root->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->val = ;
root->right->left = NULL;
root->right->right = NULL;
root->left->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->left->val = ;
root->left->left->left = NULL;
root->left->left->right = NULL;
root->left->right = (TreeNode*)malloc(sizeof(TreeNode));
root->left->right->val = ;
root->left->right->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->right->left->val = ;
root->left->right->left->left = NULL;
root->left->right->left->right = NULL;
root->left->right->right = (TreeNode*)malloc(sizeof(TreeNode));
root->left->right->right->val = ;
root->left->right->right->left = NULL;
root->left->right->right->right = NULL; return root;
} void Mirror(TreeNode* root)
{
if (root == NULL)
return;
if (root->left==NULL && root->right==NULL)
return; TreeNode* temp = root->left;
root->left = root->right;
root->right = temp; if (root->left)
Mirror(root->left);
if (root->right)
Mirror(root->right);
} // 层序遍历二叉树
void PrintTreeByLevel(TreeNode* root)
{
if (root == NULL)
return; vector<TreeNode*> vec;
vec.push_back(root);
int cur = ;
int last = ; while (cur < vec.size())
{
last = vec.size();
while (cur < last)
{
printf("%d ", vec[cur]->val);
if (vec[cur]->left != NULL)
vec.push_back(vec[cur]->left);
if (vec[cur]->right != NULL)
vec.push_back(vec[cur]->right); ++ cur;
}
printf("\n");
}
} int main()
{
TreeNode* tree = createTree(); PrintTreeByLevel(tree);
printf("\n"); Mirror(tree);
PrintTreeByLevel(tree); return ;
}

剑指Offer17 二叉树的镜像的更多相关文章

  1. 剑指offer——二叉树的镜像

    题目:操作给定的二叉树,将其变换为源二叉树的镜像. 思路:前序(根左右的顺序)遍历一棵树,在存储的时候将其左右树进行交换,最后按照处理后的树还原,即得到其镜像. /** public class Tr ...

  2. 剑指Offer 二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  3. 剑指offer--25.二叉树的镜像

    时间限制:1秒 空间限制:32768K 热度指数:238655 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 ...

  4. 剑指Offer-18.二叉树的镜像(C++/Java)

    题目: 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ ...

  5. 用js刷剑指offer(二叉树的镜像)

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  6. 剑指Offer——二叉树

    剑指Offer--二叉树 前言 数据结构通常是编程面试中考察的重点.在参加面试之前,应聘者需要熟练掌握链表.树.栈.队列和哈希表等数据结构,以及它们的操作.本片博文主要讲解二叉树操作的相关知识,主要包 ...

  7. JS数据结构与算法 - 剑指offer二叉树算法题汇总

    ❗❗ 必看经验 在博主刷题期间,基本上是碰到一道二叉树就不会碰到一道就不会,有时候一个下午都在搞一道题,看别人解题思路就算能看懂,自己写就呵呵了.一气之下不刷了,改而先去把二叉树的基础算法给搞搞懂,然 ...

  8. 剑指offer 二叉树中和为某一个值的路径

    剑指offer 牛客网 二叉树中和为某一个值的路径 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 15:53:58 2 ...

  9. 剑指offer 二叉树的层序遍历

    剑指offer 牛客网 二叉树的层序遍历 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 09:33:16 2019 @ ...

随机推荐

  1. 小巧实用js倒计时

    <script type="text/javascript">     var intDiff = parseInt(15); //倒计时总秒数量     functi ...

  2. android ExpandAbleListView控件

    ExpandAbleListView控件 1.API对ExpandAbleListView的解释:

  3. memcached Logging

    For reasons now relegated to history, Spy has its own logging implementation. However, it is compati ...

  4. 理解Web标准(网站标准)

    我觉得一名Web前端应该好好理解Web标准到底是什么,为什么要在我们的实际实践中遵循Web标准. 什么是Web标准.百度百科的解释是: WEB标准不是某一个标准,而是一系列标准的集合.网页主要由三部分 ...

  5. IE调试方法(一)<转>

    前面两篇关于IE11开发人员工具的文章,我们分别介绍了两个新的功能:UI响应工具和内存分析工具,今天为大家介绍一个老功能:网络工具,虽然是在IE9开始已经加入了这个工具,但是在IE11中还有有很多改进 ...

  6. Swift学习笔记八

    函数 Swift的函数语法非常独特,也提供了很高的灵活性和可读性.它可以充分表达从简单的无参数C风格函数到复杂的拥有局部变量和外部变量的OC风格的方法.参数可以有默认值,方便函数的调用.Swift中的 ...

  7. 几种Java写webservice的比较

    Java6,Axis2.XFire.CXF 1.JWS是Java语言对WebService服务的一种实现,用来开发和发布服务.而从服务本身的角度来看JWS服务是没有语言界限的.但是Java语言为Jav ...

  8. OpenGL 4.0 GLSL 实现 投影纹理映射(Projective Texture Mapping) (转)

    http://blog.csdn.net/zhuyingqingfen/article/details/19331721   分类: GLSL  投影纹理映射 (projective texture ...

  9. [Angular2 Router] CanDeactivate Route Guard - How To Confirm If The User Wants To Exit A Route

    In this tutorial we are going to learn how we can to configure an exit guard in the Angular 2 Router ...

  10. Java Web系统经常使用的第三方接口

    1. Web Service 接口 1.1 接口方式说明和长处 在笔者的开发生涯中,当作为接口提供商给第三方提供接口时,以及作为client去调用第三方提供的接口时,大部分时候都是使用 Web  Se ...