当前位置:网站首页>LeetCode226. Flip binary tree
LeetCode226. Flip binary tree
2022-07-04 01:26:00 【Qingshan's green shirt】
LeetCode226. Flip binary tree
List of articles
1. problem

2. Ideas
The overall idea is to flip the left and right children of each node , So we need to traverse the binary tree . The focus is on how to traverse the binary tree !
Before the order 、 In the following order 、 Sequence is OK , No middle order , Because some nodes will be exchanged twice !
The next preface is an example :
3. Code implementation
(1) recursive
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
//1. Termination conditions
if(root==NULL) return NULL;
//2. Exchange left and right nodes
TreeNode* tmp=root->left;
root->left=root->right;
root->right=tmp;
//3. Traverse the left and right subtrees
invertTree(root->left);
invertTree(root->right);
return root;
}
};
Be careful :
The code of the exchange node is :
TreeNode* tmp=root->left; root->left=root->right; root->right=tmp;
Or use swap function !!!
swap(root->left, root->right);
Error code :
This is wrong !
Only the values of some two nodes are exchanged , Without completely inverting the entire subtree !TreeNode *tmpNode = new TreeNode();// Create a new nodetmpNode->val = root->right->val;// Exchange valueroot->right->val = root->left->val;root->left->val = tmpNode->val;
For example, the first step is completed , It becomes as shown in the figure below :
(2) iteration
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
stack<TreeNode*>st;
if(root == NULL) return root;
st.push(root);// The root node is in the stack !
while(!st.empty())// Stack is not empty
{
TreeNode* node = st.top();// The pointer points to the top node of the stack // in
st.pop();// Out of the stack
swap(node->left, node->right);
if(node->right) st.push(node->right);// The right son enters the stack !( Empty nodes do not stack )
if(node->left) st.push(node->left);// Left son into the stack !( Empty nodes do not stack )
}
return root; // The stack is empty. , end
}
};
边栏推荐
- Related configuration commands of Huawei rip
- 长文综述:大脑中的熵、自由能、对称性和动力学
- Flutter local database sqflite
- Oracle database knowledge points that cannot be learned (II)
- Msp32c3 board connection MSSQL method
- 功能:求出菲波那契数列的前一项与后一项之比的极限的 近似值。例如:当误差为0.0001时,函数值为0.618056。
- Oracle database knowledge points that cannot be learned (III)
- Typescript basic knowledge sorting
- C library function int fprintf (file *stream, const char *format,...) Send formatted output to stream
- Weekly open source project recommendation plan
猜你喜欢

Force buckle day32

Gee: create a new feature and set corresponding attributes

MySQL deadly serial question 2 -- are you familiar with MySQL index?

Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?

Make drop-down menu

The super fully automated test learning materials sorted out after a long talk with a Tencent eight year old test all night! (full of dry goods

1-Redis架构设计到使用场景-四种部署运行模式(上)

MySQL introduction - functions (various function statistics, exercises, details, tables)

TP5 automatic registration hook mechanism hook extension, with a complete case

I don't care about you. OKR or KPI, PPT is easy for you
随机推荐
Customize redistemplate tool class
QML add gradient animation during state transition
Lightweight Pyramid Networks for Image Deraining
Swagger2 quick start and use
Hash table, string hash (special KMP)
CLP information - how does the digital transformation of credit business change from star to finger?
MySQL deadly serial question 2 -- are you familiar with MySQL index?
Analysis and solution of lazyinitializationexception
使用dnSpy对无源码EXE或DLL进行反编译并且修改
GUI application: socket network chat room
关于 uintptr_t和intptr_t 类型
AI helps make new breakthroughs in art design plagiarism retrieval! Professor Liu Fang's team paper was employed by ACM mm, a multimedia top-level conference
基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
[prefix and notes] prefix and introduction and use
Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading
0 basic learning C language - nixie tube dynamic scanning display
Trading software programming
2020-12-02 SSM advanced integration Shang Silicon Valley
Fundamentals of machine learning: feature selection with lasso
Cloud dial test helps Weidong cloud education to comprehensively improve the global user experience
