当前位置:网站首页>Leetcode-226. Invert Binary Tree
Leetcode-226. Invert Binary Tree
2022-07-07 07:30:00 【Eistert】
subject
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
solution
Method 1 : recursive
Ideas and algorithms
This is a classic binary tree problem . obviously , We start at the root node , Recursively traverse the tree , And start flipping from the leaf node . If the node currently traversed root The two subtrees on the left and right have been turned over , Then we just need to exchange the positions of the two subtrees , Can be completed to root Is the flip of the entire subtree of the root node .
Code
/** * 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.right = left;
root.left = right;
return root;
}
}
source
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/invert-binary-tree
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
边栏推荐
- L'étape avancée du pointeur de langage C (haut de gamme) pour l'enroulement des cocons
- $parent (get parent component) and $root (get root component)
- Outsourcing for four years, abandoned
- 外包干了三年,废了...
- Flexible layout (II)
- Talk about seven ways to realize asynchronous programming
- $refs: get the element object or sub component instance in the component:
- How do I get the last part of a string- How to get the last part of a string?
- Music | cat and mouse -- classic not only plot
- 抽丝剥茧C语言(高阶)指针的进阶
猜你喜欢

Kuboard can't send email and nail alarm problem is solved

Role of virtual machine

Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.9 introduction to network interface (IX) extending the request3 met

"Xiaodeng in operation and maintenance" meets the compliance requirements of gdpr

Lm11 reconstruction of K-line and construction of timing trading strategy

Jesd204b clock network

Music | cat and mouse -- classic not only plot

MIPS uclibc cross compile ffmpeg, support g711a encoding and decoding

Model application of time series analysis - stock price prediction

Implementing data dictionary with JSP custom tag
随机推荐
$refs: get the element object or sub component instance in the component:
Paranoid unqualified company
四、高性能 Go 语言发行版优化与落地实践 青训营笔记
sql中对集合进行非空校验
Abnova immunohistochemical service solution
Non empty verification of collection in SQL
Apache AB stress test
ViewModelProvider. Of obsolete solution
Flexible layout (II)
修改Jupyter Notebook文件路径
Cloud backup project
ROS2规划系统plansys2简单的例子
三、高质量编程与性能调优实战 青训营笔记
L'étape avancée du pointeur de langage C (haut de gamme) pour l'enroulement des cocons
深度学习花书+机器学习西瓜书电子版我找到了
Detailed explanation of neo4j installation process
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
Dynamics CRM server deployment - restore database prompt: the database is in use
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
PostgreSQL source code (60) transaction system summary