当前位置:网站首页>Leetcode 226. flip binary tree
Leetcode 226. flip binary tree
2022-07-24 22:02:00 【LuZhouShiLi】
Leetcode 226. Flip binary tree
subject
Give you the root node of a binary tree root , Flip this binary tree , And return its root node .
Ideas
- The first sequence traversal , First swap the left and right child nodes
- Then recursively exchange left and right subtrees
Code
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
// Recursive export
if(root == NULL) return root;
// The first sequence traversal First swap the left and right child nodes Then flip the left subtree and the right subtree
swap(root->left,root->right);
invertTree(root->left);
invertTree(root->right);
return root;
}
};
边栏推荐
- Using gcc to avoid stack smash attack
- 陈春花与莫言,都有苦难言
- [good question with two points]
- Jenkins introduction
- Conditional judgment of Shell Foundation
- Mysql database commands
- Composability and Recursion in snarkyJS
- String matching (Huawei)
- 微信小程序监听实时地理位置变化事件接口申请
- Brand new: the latest ranking of programming languages in July
猜你喜欢

Composability and Recursion in snarkyJS

Gather relevant knowledge points and expand supplements

Binary search

Conditional judgment of Shell Foundation

Circom 2.0: A Scalable Circuit Compiler

【考研英语词汇训练营】Day 11 —— offer ,form ,maintain ,critical

数据库之-元数据 DatabaseMetaData 初学

rogabet note 1.1

leetcode:不可能得到的最短骰子序列【思维题 + 分组思想】

Easily make 2D tile map with unity tilemap - Basics
随机推荐
What should I do to select the method of mongodb instance accessing the database?
Machine learning kmeans
【南瓜书ML】(task4)神经网络中的数学推导
Shell introduction and variable definition
数据库之-元数据 DatabaseMetaData 初学
How does redis realize inventory deduction and prevent oversold? (glory Collection Edition)
MySQL forced indexing
Volcano engine releases cloud growth solutions for six industries
【考研英语词汇训练营】Day 11 —— offer ,form ,maintain ,critical
Brand new: the latest ranking of programming languages in July
Applet location interface application
Clever use of sort (list & lt; T & gt;, comparator & lt;? Super T & gt;) comparator
With this PDF, I successfully got offers from ant, jd.com, Xiaomi, Tencent and other major manufacturers
Gradle learning - integration of gradle and idea
[combination of classes (define a class in a class)]
[development tutorial 6] crazy shell arm function mobile phone - interruption experiment tutorial
PR 2022 22.5 Chinese version
"Iruntime": undeclared identifier
OSI architecture and protocols at all levels
How much does it cost to build your own personal server