当前位置:网站首页>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
}
};
边栏推荐
- Print diamond pattern
- MySQL -- Introduction and use of single line functions
- 技术实践|线上故障分析及解决方法(上)
- MySQL introduction - functions (various function statistics, exercises, details, tables)
- Msp32c3 board connection MSSQL method
- All in one 1407: stupid monkey
- [common error] custom IP instantiation error
- Hbuilder link Xiaoyao simulator
- 机器学习基础:用 Lasso 做特征选择
- ThinkPHP uses redis to update database tables
猜你喜欢

MPLS experiment

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

我管你什么okr还是kpi,PPT轻松交给你

Introduction to A-frame virtual reality development
![[turn] solve the problem of](/img/c2/368582a8ed26254409fe391899ba41.jpg)
[turn] solve the problem of "RSA public key not find" appearing in Navicat premium 15 registration

be based on. NETCORE development blog project starblog - (14) realize theme switching function

Network layer - routing
![[dynamic programming] leetcode 53: maximum subarray sum](/img/f0/80357f9ffc556f3ed4d3aa0901bb1d.jpg)
[dynamic programming] leetcode 53: maximum subarray sum

A malware detection method for checking PLC system using satisfiability modulus theoretical model

Function: find the sum of the elements on the main and sub diagonal of the matrix with 5 rows and 5 columns. Note that the elements where the two diagonals intersect are added only once. For example,
随机推荐
Some other configurations on Huawei's spanning tree
be based on. NETCORE development blog project starblog - (14) realize theme switching function
Huawei BFD and NQA
Oracle database knowledge points (IV)
A malware detection method for checking PLC system using satisfiability modulus theoretical model
Meta metauniverse female safety problems occur frequently. How to solve the related problems in the metauniverse?
Luogu p1309 Swiss wheel
be based on. NETCORE development blog project starblog - (14) realize theme switching function
Gnupg website
Huawei rip and BFD linkage
【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
Typescript basic knowledge sorting
2020-12-02 SSM advanced integration Shang Silicon Valley
Windos10 reinstallation system tutorial
在寻求人类智能AI的过程中,Meta将赌注押向了自监督学习
Employees' turnover intention is under the control of the company. After the dispute, the monitoring system developer quietly removed the relevant services
Weekly open source project recommendation plan
Release and visualization of related data
MySQL -- Introduction and use of single line functions
【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
