当前位置:网站首页>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
}
};
边栏推荐
- A little understanding of GSLB (global server load balance) technology
- be based on. NETCORE development blog project starblog - (14) realize theme switching function
- Since the "epidemic", we have adhered to the "no closing" of data middle office services
- Special copy UML notes
- Typescript basic knowledge sorting
- 技術實踐|線上故障分析及解决方法(上)
- Audio resource settings for U3D resource management
- C import Xls data method summary V (complete code)
- Introduction to superresolution
- 求esp32C3板子连接mssql方法
猜你喜欢
Some other configurations on Huawei's spanning tree
[turn] solve the problem of "RSA public key not find" appearing in Navicat premium 15 registration
Huawei rip and BFD linkage
Make drop-down menu
[common error] custom IP instantiation error
Function: store the strings entered in the main function in reverse order. For example, if you input the string "ABCDEFG", you should output "gfedcba".
Openbionics exoskeleton project introduction | bciduino community finishing
I don't care about you. OKR or KPI, PPT is easy for you
swagger中响应参数为Boolean或是integer如何设置响应描述信息
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
随机推荐
CesiumJS 2022^ 源码解读[8] - 资源封装与多线程
Oracle database knowledge points that cannot be learned (II)
Analysis and solution of lazyinitializationexception
Future源码一观-JUC系列
Characteristics of ginger
HackTheBox-baby breaking grad
Huawei BFD and NQA
Design of database table foreign key
TP5 automatic registration hook mechanism hook extension, with a complete case
Sequence list and linked list
机器学习基础:用 Lasso 做特征选择
The force deduction method summarizes the single elements in the 540 ordered array
【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
Function: write function fun to find s=1^k+2^k +3^k ++ The value of n^k, (the cumulative sum of the K power of 1 to the K power of n).
Conditional test, if, case conditional test statements of shell script
File contains vulnerability summary
Technical practice online fault analysis and solutions (Part 1)
使用dnSpy对无源码EXE或DLL进行反编译并且修改
2-redis architecture design to use scenarios - four deployment and operation modes (Part 2)
Summary of common tools and technical points of PMP examination