当前位置:网站首页>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 node
tmpNode->val = root->right->val;// Exchange value
root->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
}
};
边栏推荐
- How to delete MySQL components using xshell7?
- Characteristics of ginger
- Is Shengang securities company as safe as other securities companies
- Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them
- Introduction to A-frame virtual reality development
- Future源码一观-JUC系列
- Some other configurations on Huawei's spanning tree
- Customize redistemplate tool class
- Trading software programming
- [turn] solve the problem of "RSA public key not find" appearing in Navicat premium 15 registration
猜你喜欢
Technical practice online fault analysis and solutions (Part 1)
Sequence list and linked list
长文综述:大脑中的熵、自由能、对称性和动力学
Analysis and solution of lazyinitializationexception
1-Redis架构设计到使用场景-四种部署运行模式(上)
Lightweight Pyramid Networks for Image Deraining
【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
0 basic learning C language - nixie tube dynamic scanning display
查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
Who moved my code!
随机推荐
A malware detection method for checking PLC system using satisfiability modulus theoretical model
Summary of JWT related knowledge
C import Xls data method summary II (save the uploaded file to the DataTable instance object)
MPLS experiment
中电资讯-信贷业务数字化转型如何从星空到指尖?
Unity Shader入门精要读书笔记 第三章 Unity Shader基础
【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
Install the pit that the electron has stepped on
使用dnSpy对无源码EXE或DLL进行反编译并且修改
【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
Openbionics robot project introduction | bciduino community finishing
51 MCU external interrupt
PMP 考试常见工具与技术点总结
Some other configurations on Huawei's spanning tree
How to use AHAS to ensure the stability of Web services?
[common error] UART cannot receive data error
SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution
Luogu p1309 Swiss wheel
Pratique technique | analyse et solution des défaillances en ligne (Partie 1)
C import Xls data method summary III (processing data in datatable)