当前位置:网站首页>Leetcode 226 翻转二叉树(递归)
Leetcode 226 翻转二叉树(递归)
2022-07-27 14:44:00 【w͏l͏j͏】
难度简单573
翻转一棵二叉树。
示例:
输入:
4 / \ 2 7 / \ / \ 1 3 6 9输出:
4 / \ 7 2 / \ / \ 9 6 3 1
思路:递归翻转就好,先翻转根,然后翻转左子树,然后翻转右子树。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==NULL){return NULL;}
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
invertTree(root->left);
invertTree(root->right);
return root;
}
}; 
边栏推荐
- [sword finger offer] interview question 42: the maximum sum of continuous subarrays -- with 0x80000000 and int_ MIN
- Solve mt7620 continuous cycle uboot (LZMA error 1 - must reset board to recover)
- DRF学习笔记(五):视图集ViewSet
- [sword finger offer] interview question 56-i: the number of numbers in the array I
- Short video mall system, system prompt box, confirmation box, click blank to close the pop-up box
- C language: dynamic memory function
- Division of entity classes (VO, do, dto)
- Breaking through soft and hard barriers, Xilinx releases Vitis unified software platform for developers
- Live broadcast software development, customized pop-up effect of tools
- Openwrt adds support for SD card
猜你喜欢

Single machine high concurrency model design

C language: function stack frame
![[sword finger offer] interview question 49: ugly number](/img/7a/2bc9306578530fbb5ac3b32254676d.png)
[sword finger offer] interview question 49: ugly number

Binary Insertion Sort

SQL multi table query

时间序列-ARIMA模型

Mapreduce实例(一):WordCount

DRF学习笔记(二):数据反序列化
![[sword finger offer] interview question 50: the first character that appears only once - hash table lookup](/img/72/b35bdf9bde72423410e365e5b6c20e.png)
[sword finger offer] interview question 50: the first character that appears only once - hash table lookup

MySQL表数据的增删查改
随机推荐
[sword finger offer] interview question 53-i: find the number 1 in the sorted array -- three templates for binary search
单机高并发模型设计
Constraints, design and joint query of data table -- 8000 word strategy + Exercise answers
初识MySQL数据库
flume增量采集mysql数据到kafka
移动端h5常见问题
禁令之下,安防巨头海康与大华的应对之策!
centos yum方式安装mysql
Single machine high concurrency model design
为应对RISC-V挑战?Arm CPU引入自定义指令功能!
传音控股披露被华为起诉一事:已立案,涉案金额2000万元
突破软硬壁垒,赛灵思面向开发者发布Vitis统一软件平台
The risk of multithreading -- thread safety
Paper_Book
__typeof和typeof的差异
Insert sort directly
C language: minesweeping games
Easy to understand, distinguish between ++i and I++
[sword finger offer] interview question 56-i: the number of numbers in the array I
实现浅拷贝和深拷贝+