博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
剑指 Offer 27. 二叉树的镜像
阅读量:4033 次
发布时间:2019-05-24

本文共 942 字,大约阅读时间需要 3 分钟。

题目描述

请完成一个函数,输入一个二叉树,该函数输出它的镜像。

例如输入:

4

/

2 7
/ \ /
1 3 6 9
镜像输出:

4

/

7 2
/ \ /
9 6 3 1

示例 1:

输入:root = [4,2,7,1,3,6,9]

输出:[4,7,2,9,6,3,1]

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

JAVA

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */ /*递归实现 */class Solution {
public TreeNode mirrorTree(TreeNode root) {
//递归停止条件 if(root==null) return root; if(root.left==null && root.right==null) return root; //本级递归干什么 TreeNode temp=root.left; root.left=root.right; root.right=temp; if(root.left!=null) root.left=mirrorTree(root.left); if(root.right!=null) root.right=mirrorTree(root.right); //递归返回值 return root; }}
你可能感兴趣的文章
解决国内NPM安装依赖速度慢问题
查看>>
Brackets安装及常用插件安装
查看>>
Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
查看>>
fastcgi_param 详解
查看>>
Nginx配置文件(nginx.conf)配置详解
查看>>
标记一下
查看>>
一个ahk小函数, 实现版本号的比较
查看>>
IP报文格式学习笔记
查看>>
autohotkey快捷键显示隐藏文件和文件扩展名
查看>>
Linux中的进程
查看>>
学习python(1)——环境与常识
查看>>
学习设计模式(3)——单例模式和类的成员函数中的静态变量的作用域
查看>>
自然计算时间复杂度杂谈
查看>>
当前主要目标和工作
查看>>
Intellij IDEA启动优化,让开发的感觉飞起来
查看>>
使用 Springboot 对 Kettle 进行调度开发
查看>>
如何优雅的编程,lombok你怎么这么好用
查看>>
一文看清HBase的使用场景
查看>>
除了负载均衡,Nginx还可以做很多,限流、缓存、黑白名单
查看>>
解析zookeeper的工作流程
查看>>