当前位置:网站首页>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 51. reverse pairs in the array
- Reduce program ROM ram, GCC -ffunction sections -fdata sections -wl, – detailed explanation of GC sections parameters
- Addition, deletion, query and modification of MySQL table data
- Scratch crawler framework
- 文本截取图片(哪吒之魔童降世壁纸)
- Binary Insertion Sort
- Multiline text overflow dotting
- [sword finger offer] interview question 56-i: the number of numbers in the array I
- First understanding of structure
- vant-ui toast和dialog使用
猜你喜欢

Clickhouse 20.x distributed table testing and chproxy deployment (II)

多线程带来的的风险——线程安全

Ncnn reasoning framework installation; Onnx to ncnn

Openwrt adds support for SD card
![[sword finger offer] interview question 53-i: find the number 1 in the sorted array -- three templates for binary search](/img/4b/460ac517e9a5d840a0961f5d7d8c9d.png)
[sword finger offer] interview question 53-i: find the number 1 in the sorted array -- three templates for binary search

Pycharm导入已有的Project

scrapy爬虫框架

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

leetcode234题-简单方法判断回文链表

Single machine high concurrency model design
随机推荐
Scratch crawler framework
A powerful web vulnerability scanning and verification tool (vulmap)
微信小程序个人号开通流量主
使用transform:translate()出现内容模糊问题
[sword finger offer] interview question 39: numbers that appear more than half of the time in the array
解决MT7620不断循环uboot(LZMA ERROR 1 - must RESET board to recover)
剑指 Offer 51. 数组中的逆序对
为应对RISC-V挑战?Arm CPU引入自定义指令功能!
可载100人!马斯克发布史上最强“星际飞船” !最早明年上火星!
CAS compares the knowledge exchanged, ABA problems, and the process of lock upgrading
判断数据的精确类型
初识MySQL数据库
Samsung closes its last mobile phone factory in China
DRF学习笔记(准备)
[sword finger offer] interview question 53- Ⅱ: missing numbers in 0 ~ n-1 - binary search
渗透测试-干货 | 80篇+网络安全面试经验帖(面试篇)
The risk of multithreading -- thread safety
openwrt 编译驱动模块(在openwrt源代码外部任意位置编写代码,独立模块化编译.ko)
Baidu picture copy picture address
Join hands with sifive, Galanz will enter the semiconductor field! Exposure of two self-developed chips