当前位置:网站首页>[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 *〜(*≧ω≦)
边栏推荐
- R language ggplot2 visualization: use the ggboxplot function of ggpubr package to visualize the box diagram and customize the fill parameter to configure the filling color of the box
- tolua之wrap文件的原理与使用
- AlexNet—论文分析及复现
- Static proxy instance
- Solutions to slow start of MATLAB
- CentOS 7 install MySQL 5.7 & uninstall MySQL 5.7
- Saltstack command injection vulnerability analysis (cve-2020-16846)
- Modify the running container port mapping
- Introduction to the usage of SAP ui5 image display control avatar trial version
- An idea plug-in helps you transform dto, VO, Bo, Po, do gracefully
猜你喜欢

15. User web layer services (III)

Test platform (V) knowledge points supplement

Ruiji takeout - day01

A new mode of one-stop fixed asset management

A hundred flowers bloom in data analysis engines. Why invest heavily in Clickhouse?

Untiy controls the playback speed of animation

Develop your own NPM package from 0

Service workers let the website dynamically load webp pictures

Untiy中控制Animation的播放速度

Unity one key replacement of objects in the scene
随机推荐
14、用户web层服务(二)
14. User web layer services (II)
【补题日记】[2022牛客暑期多校2]H-Take the Elevator
A new mode of one-stop fixed asset management
P5472 [NOI2019] 斗主地(期望、数学)
“蔚来杯“2022牛客暑期多校训练营2
Guys, ask me, this can't be checkpoint, because there is a JDBC task whose task status is finished,
中国业务型CDP白皮书 | 爱分析报告
Lua 中 __index、__newindex、rawget、rawset的理解
[pyGame practice] when the end of the world comes, how long can you live in a cruel survival game that really starts from scratch?
Hcip day 6 (OSPF related knowledge)
Code simplification
Hcip (condition matching and OSPF packet related knowledge)
Hcip (PAP authentication and chap authentication of PPP)
PFP会是数字藏品的未来吗?
Reasons and solutions for moving the first column to the last column in El table
Database advanced learning notes - system package
Traversal and copy of files in jar package
[diary of supplementary questions] [2022 Niuke summer school 2] h-take the elevator
R language uses dplyr package group_ By function and summarize function calculate the mean value of all covariates involved in the analysis based on grouped variables (difference in means of covariate