二叉树的镜像

二叉树的镜像

  1. 给定一个二叉树,输出二叉树的镜像。
  2. 只需要使用一个简单的递归,分别对左右子树反转后再对当前结点进行反转。
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// 4
// / \
// 2 7
// / \ / \
// 1 3 6 9
class Solution {
public:
void reverse(TreeNode* node){
if(node->left){
reverse(node->left);
}
if(node->right){
reverse(node->right);
}
TreeNode* temp=node->left;
node->left=node->right;
node->right=temp;
}
TreeNode* mirrorTree(TreeNode* root) {
if(root)//root不为null
reverse(root);
return root;
}
};
int main(){
TreeNode* t1=new TreeNode(1);
TreeNode* t2=new TreeNode(2);
TreeNode* t3=new TreeNode(3);
TreeNode* t4=new TreeNode(4);
TreeNode* t6=new TreeNode(6);
TreeNode* t7=new TreeNode(7);
TreeNode* t9=new TreeNode(9);
t7->left=t6;t7->right=t9;
t2->left=t1;t2->right=t3;
t4->left=t2;t4->right=t7;
Solution solution;
TreeNode* root=solution.mirrorTree(t4);
cout<<root->left->val<<" "<<root->right->val<<endl;
system("pause");
return 0;
}

LeetCode-二叉树的镜像的更多相关文章

  1. 【剑指Offer】二叉树的镜像 解题报告(Python)

    [剑指Offer]二叉树的镜像 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://www.nowcoder.com/ta/coding-interviews 题 ...

  2. 剑指Offer面试题:18.二叉树的镜像

    一.题目:二叉树的镜像 题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像.例如下图所示,左图是原二叉树,而右图则是该二叉树的镜像. 该二叉树节点的定义如下,采用C#语言描述: public c ...

  3. 剑指Offer 二叉树的镜像

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

  4. 剑指Offer:面试题19——二叉树的镜像(java实现)

    问题描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树结点定义为: public class TreeNode { int val = 0; TreeNode left = null; Tr ...

  5. (剑指Offer)面试题19:二叉树的镜像

    题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的定义如下: struct TreeNode{ int val; TreeNode* left; TreeNode* right; }; 输 ...

  6. 《剑指offer》— JavaScript(18)二叉树的镜像

    二叉树的镜像 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 相关知识 二叉树的镜像定义: 源二叉树 镜像二叉树 思路 有关二叉树的算法问题,一般都可以通过递归来解决.那么写一个正确的递归程序 ...

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

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

  8. 包含min函数的栈 ,二叉树的镜像

    包含min函数的栈 问题 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 代码 # -*- coding:utf-8 -*- class Sol ...

  9. 《剑指offer》 二叉树的镜像

    本题来自<剑指offer>二叉树的镜像 题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 ...

  10. 剑指offer(18)二叉树的镜像

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

随机推荐

  1. hdu 6822 Paperfolding 规律+排列组合+逆元

    题意: 给你一片纸,你可以对它进行四种操作,分别是向上.向下.向左.向右对折.把对折之后的纸片横向剪开,再纵向剪开(十字架剪开) 问你你能剪出来的纸片的期望个数 题解(参考:https://blog. ...

  2. SPOJ 227 Ordering the Soldiers

    As you are probably well aware, in Byteland it is always the military officer's main worry to order ...

  3. cmd控制台Windows服务相关

    1.创建服务 sc create ServerName binpath= "E:\MySql5.5\bin\mysqld.exe" 2.启动服务 sc start ServerNa ...

  4. 详解Go语言I/O多路复用netpoller模型

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的go的源码15.7 可以从 Go 源码目录结构和对应代码文件了解 Go 在不同 ...

  5. K8S(14)监控实战-grafana出图_alert告警

    k8s监控实战-grafana出图_alert告警 目录 k8s监控实战-grafana出图_alert告警 1 使用炫酷的grafana出图 1.1 部署grafana 1.1.1 准备镜像 1.1 ...

  6. Zabbix 微信监控报警

    Zabbix-Server 设置 # 使脚本目录生效 [root@zabbix ~]# grep 'script' /etc/zabbix/zabbix_server.conf # AlertScri ...

  7. 8.rabbitmq RPC模拟微服务架构中的服务调用

    标题 : 8.rabbitmq RPC模拟微服务架构中的服务调用 目录 : RabbitMQ 序号 : 8 { var connectionFactory = new ConnectionFactor ...

  8. Java 并发机制底层实现 —— volatile 原理、synchronize 锁优化机制

    本书部分摘自<Java 并发编程的艺术> 概述 相信大家都很熟悉如何使用 Java 编写处理并发的代码,也知道 Java 代码在编译后变成 Class 字节码,字节码被类加载器加载到 JV ...

  9. Error Code: 1055.Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.

    环境:mysql-8.0.15-winx64 问题描述: Error querying database. Cause: java.sql.SQLSyntaxErrorException: Expre ...

  10. nyoj-2357

    2357: 插塔憋憋乐 时间限制: 1 秒  内存限制: 128 MB提交: 107  解决: 28提交 状态 题目描述 众所不知,LLM是一位红警3大佬,打的非常厉害,但是曾经也是一位萌新,喜欢在家 ...