当前位置:网站首页>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,从头到尾三分钟都没到。写完都迷迷糊糊的。
用递归做
- 先判断当前结点是否为空
- 交换左右结点
- 进入左结点执行这个步骤
- 进入右结点执行这个步骤
- 最后看看题解
吓我一跳。。。赶紧再看看。
边栏推荐
- Small record of thinking
- 用于TCP协议交互的TCPClientDemo
- Redis cluster view the slots of each node
- 【华为云IoT】读书笔记之《万物互联:物联网核心技术与安全》第3章(上)
- Deep thinking on investment
- 【微服务|openfeign】feign的两种降级方式|Fallback|FallbackFactory
- postgresql 用户不能自己创建表格配置
- ctf-pikachu-XSS
- Support the first triggered go ticker
- Katalon框架测试web(二十一)获取元素属性断言
猜你喜欢
图解网络:什么是热备份路由器协议HSRP?
SQL语句加强练习(MySQL8.0为例)
AAAI2022 | Word Embeddings via Causal Inference: Gender Bias Reducing and Semantic Information Preserving
Objective-C description method and type method
Pytest multi process / multi thread execution test case
Detailed explanation of PPTC self recovery fuse
2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
用于TCP协议交互的TCPClientDemo
Katalon框架测试web(二十六)自动发邮件
Katalon中控件的参数化
随机推荐
Deep thinking on investment
Pandora IOT development board learning (HAL Library) - Experiment 6 independent watchdog experiment (learning notes)
Sales management system of lightweight enterprises based on PHP
支持首次触发的 Go Ticker
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
Select sorting and bubble sorting template
【微服务|openfeign】使用openfeign远程调用文件上传接口
10 reasons for not choosing to use free virtual hosts
Myslq delete followed by limit
Smart subway | cloud computing injects wisdom into urban subway transportation
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
"Implement both software and hardware" to help build a new cloud computing data center
Pointer array and array pointer
[Logitech] m720
vim正确加区间注释
Parameterization of controls in katalon
01 QEMU starts the compiled image vfs: unable to mount root FS on unknown block (0,0)
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (I)
渗透实战-SQLServer提权
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。