当前位置:网站首页>[leetcode] 6. invert binary tree
[leetcode] 6. invert binary tree
2022-07-28 12:03:00 【AQin1012】
Title Description
Description in English
Given the root of a binary tree, invert the tree, and return its root. Example 1:

Input: root = [4,2,7,1,3,6,9] Output: [4,7,2,9,6,3,1] Example 2:

Input: root = [2,1,3] Output: [2,3,1] Example 3: Input: root = [] Output: [] Constraints:
The number of nodes in the tree is in the range [0, 100].
-100 <= Node.val <= 100
English address
https://leetcode.com/problems/invert-binary-tree/
Description of Chinese version
Give you the root node of a binary tree root , Flip this binary tree , And return its root node .
Tips :
The number of nodes in the tree ranges from [0, 100] Inside
-100 <= Node.val <= 100
Address of Chinese version
https://leetcode.cn/problems/invert-binary-tree/
Their thinking
Start at the root node , Exchange left and right subtrees ( Before looking at the answer , I am the default binary tree, and the non leaf nodes have left and right subtrees , But I tried several cases that never passed and found , Nodes that are not entered are used null Instead of ), Then until the leaf node , Because the operation of the left and right subtrees in the middle exchange is the same , Consider recursive calls .
How to solve the problem
My version

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root != null) {
if (root.left != null) {
if (root.right != null) {
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
invertTree(root.left);
invertTree(root.right);
} else {
root.right = root.left;
root.left = null;
invertTree(root.right);
}
} else {
root.left = root.right;
root.right = null;
invertTree(root.left);
}
}
return root;
}
}Official edition
recursive

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
}
}summary
As recorded in the problem-solving ideas , Before looking at the answer , I didn't expect the missing node to be used null Instead of , So use the root != null) && root.left != null&&root.right != null The judgment of the , If null And ignore , But I tried several times and failed , From these cases , You can use null Replace the missing node , So I made a modification . The official recursive method is also used , But in contrast, the efficiency is higher , The code is simpler .
Welcome students with better ideas to kick me in the comment area *〜(*≧ω≦)
边栏推荐
- OSCache cache monitoring Refresh Tool
- Modify the running container port mapping
- Deployment and use of Minio distributed object storage
- Minikube initial experience environment construction
- Character function and string function (Part 1)
- How async await implements concurrency
- 中国业务型CDP白皮书 | 爱分析报告
- [pyGame practice] the super interesting bubble game is coming - may you be childlike and always happy and simple~
- 2022.07.06 summer training personal qualifying (I)
- Article summary of MinGW installation and use
猜你喜欢

Redis installation

The game process and the underlying implementation are gradually completed

Style conversion model style_ Transformer project instance pytorch implementation

Shell (II)

A hundred flowers bloom in data analysis engines. Why invest heavily in Clickhouse?
![[general database integrated development environment] Shanghai daoning provides you with Aqua Data Studio downloads, tutorials, and trials](/img/46/830b7703ae7cbfa6051137061560c2.png)
[general database integrated development environment] Shanghai daoning provides you with Aqua Data Studio downloads, tutorials, and trials

Lua middle__ index、__ Understanding of newindex, rawget and rawset

108. Introduction to the usage of SAP ui5 image display control Avatar

移动端人脸风格化技术的应用

Reasons and solutions for moving the first column to the last column in El table
随机推荐
How async await implements concurrency
Can dynamic partitions be configured when MySQL is offline synchronized to ODPs
108. Introduction to the usage of SAP ui5 image display control Avatar
2022.07.07 summer training personal qualifying (II)
R language uses LM function to build regression model, uses the augmented function of bloom package to store the model results in dataframe, and uses ggplot2 to visualize the regression residual diagr
什么是WordPress
[diary of supplementary questions] [2022 Niuke summer multi school 2] l-link with level editor I
Some knowledge concepts
In order to ensure the normal operation of fire-fighting equipment in large buildings, the power supply monitoring system of fire-fighting equipment plays a key role
Shell (I)
An example of the mandatory measures of Microsoft edge browser tracking prevention
"Weilai Cup" 2022 Niuke summer multi school training camp 2
[general database integrated development environment] Shanghai daoning provides you with Aqua Data Studio downloads, tutorials, and trials
I want to ask you guys, if there is a master-slave switch when CDC collects mysql, is there any solution
Lua 中 __index、__newindex、rawget、rawset的理解
A hundred flowers bloom in data analysis engines. Why invest heavily in Clickhouse?
[applet] how to notify users of wechat applet version update?
Untiy controls the playback speed of animation
Skiasharp's WPF self drawn drag ball (case version)
Redis安装