当前位置:网站首页>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,从头到尾三分钟都没到。写完都迷迷糊糊的。
用递归做
- 先判断当前结点是否为空
- 交换左右结点
- 进入左结点执行这个步骤
- 进入右结点执行这个步骤
- 最后看看题解
吓我一跳。。。赶紧再看看。
边栏推荐
- Idea modify body color
- [book club issue 13] packaging format of video files
- Penetration practice - sqlserver empowerment
- Unity 绘制弹球和台球的运动轨迹
- 思考的小记录
- 透过JVM-SANDBOX源码,了解字节码增强技术原理
- 如何有效远程办公之我见 | 社区征文
- Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
- There is a problem that the package cannot be parsed in the like project
- 三菱M70宏变量读取三菱M80公共变量采集三菱CNC变量读取采集三菱CNC远程刀补三菱机床在线刀补三菱数控在线测量
猜你喜欢
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
透过JVM-SANDBOX源码,了解字节码增强技术原理
There is a problem that the package cannot be parsed in the like project
SQL statement strengthening exercise (MySQL 8.0 as an example)
Parameterization of controls in katalon
02 ls 命令的具体实现
*. No main manifest attribute in jar
渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
分布式系统:what、why、how
随机推荐
[book club issue 13] multimedia processing tool ffmpeg tool set
Katalon框架测试web(二十一)获取元素属性断言
Calculate the odd sum of 1~n (1~100 as an example)
STM32外接DHT11显示温湿度
[webrtc] M98 Ninja build and compile instructions
VIM add interval annotation correctly
1289_FreeRTOS中vTaskSuspend()接口实现分析
MySQL one master multiple slaves + linear replication
Getting started with the go language is simple: go implements the Caesar password
vue多级路由嵌套怎么动态缓存组件
Parameterization of controls in katalon
The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
VIM mapping command
Database SQL statement summary, continuous update
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
Perf simple process for multithreaded profile
毕业三年,远程半年 | 社区征文
Epidemic strikes -- Thinking about telecommuting | community essay solicitation
函数计算异步任务能力介绍 - 任务触发去重
pytest多进程/多线程执行测试用例