当前位置:网站首页>leetcode刷题:二叉树05(翻转二叉树)
leetcode刷题:二叉树05(翻转二叉树)
2022-07-04 03:51:00 【涛涛英语学不进去】
226.翻转二叉树
翻转一棵二叉树。

package com.programmercarl.tree;
/** * @ClassName InvertTree * @Descriotion TODO * @Author nitaotao * @Date 2022/7/3 12:57 * @Version 1.0 * https://leetcode.cn/problems/invert-binary-tree/ * 226. 翻转二叉树 **/
public class InvertTree {
public TreeNode invertTree(TreeNode root) {
invert(root);
return root;
}
public void invert(TreeNode root) {
if (root == null) {
return;
}
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
invertTree(root.left);
invertTree(root.right);
}
}

有一说一,这题我拿过来题,一遍AC,从头到尾三分钟都没到。写完都迷迷糊糊的。
用递归做
- 先判断当前结点是否为空
- 交换左右结点
- 进入左结点执行这个步骤
- 进入右结点执行这个步骤
- 最后看看题解

吓我一跳。。。赶紧再看看。
边栏推荐
- Tcpclientdemo for TCP protocol interaction
- laravel admin里百度编辑器自定义路径和文件名
- CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
- vim映射命令
- Objective C attribute keyword
- Objective-C string class, array class
- 支持首次触发的 Go Ticker
- Global exposure and roller shutter exposure of industrial cameras
- The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
- MySQL maxscale realizes read-write separation
猜你喜欢
![[PaddleSeg 源码阅读] PaddleSeg 自定义数据类](/img/88/37c535b371486db545abc392a685af.png)
[PaddleSeg 源码阅读] PaddleSeg 自定义数据类

There is a problem that the package cannot be parsed in the like project

三年进账35.31亿,这个江西老表要IPO了

Sales management system of lightweight enterprises based on PHP

透过JVM-SANDBOX源码,了解字节码增强技术原理

函数计算异步任务能力介绍 - 任务触发去重

【罗技】m720

MySQL one master multiple slaves + linear replication

The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
![[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability](/img/46/cb5a10ffe3fcdffb7da68dbaef5b1f.png)
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
随机推荐
There is a problem that the package cannot be parsed in the like project
'2'>' 10'==true? How does JS perform implicit type conversion?
vim正确加区间注释
2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
Value transfer communication between components (parent to child, child to parent, brother component to value)
ctf-pikachu-CSRF
Select sorting and bubble sorting template
还原窗口位置的微妙之处
Go 语言入门很简单:Go 实现凯撒密码
Small record of thinking
Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption
【华为云IoT】读书笔记之《万物互联:物联网核心技术与安全》第3章(上)
三年进账35.31亿,这个江西老表要IPO了
深度优先搜索简要讲解(附带基础题)
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
Illustrated network: what is the hot backup router protocol HSRP?
【读书会第十三期】视频文件的封装格式
Penetration practice - sqlserver empowerment
mysql数据库的存储