当前位置:网站首页>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;
}
}; 
边栏推荐
- Binary Insertion Sort
- 无线网络分析有关的安全软件(aircrack-ng)
- Content ambiguity occurs when using transform:translate()
- 一款功能强大的Web漏洞扫描和验证工具(Vulmap)
- 通俗易懂地区分++i和i++
- Network principle (1) - overview of basic principles
- Taking advantage of 5g Dongfeng, does MediaTek want to fight the high-end market again?
- ARIMA模型选择与残差
- 项目优化个人感悟
- [sword finger offer] interview question 51: reverse pairs in the array - merge sort
猜你喜欢
![[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

Text capture picture (Wallpaper of Nezha's demon child coming to the world)

First understanding of structure

Half find

Pycharm导入已有的本地安装包

scrapy爬虫框架

Using Lombok results in the absence of parent class attributes in the printed toString

解决flink启动后无法正常关闭

flume增量采集mysql数据到kafka

DRF学习笔记(四):DRF视图
随机推荐
Six capabilities of test and development
Samsung closes its last mobile phone factory in China
[sword finger offer] interview question 42: the maximum sum of continuous subarrays -- with 0x80000000 and int_ MIN
网络设备硬核技术内幕 路由器篇 小结(下)
Stock account opening commission discount, stock trading account opening which securities company is good, is online account opening safe
Flask连接mysql数据库已有表
突发!海康/大华/商汤/旷视/依图/科大讯飞等28家中国实体被美列入黑名单
makefile 中指定程序运行时加载的库文件路径
Makefile specifies the path of the library file loaded when the program runs
禁令之下,安防巨头海康与大华的应对之策!
时间序列——使用tsfresh进行分类任务
mysql设置密码时报错 Your password does not satisfy the current policy requirements(修改·mysql密码策略设置简单密码)
[sword finger offer] interview question 39: numbers that appear more than half of the time in the array
Scratch crawler framework
Text capture picture (Wallpaper of Nezha's demon child coming to the world)
Understand │ what is cross domain? How to solve cross domain problems?
C language: string function and memory function
[sword finger offer] interview question 56-i: the number of numbers in the array I
__typeof和typeof的差异
C language: function stack frame