当前位置:网站首页>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 .
边栏推荐
- Example of Pushlet using handle of Pushlet
- LC interview question 02.07 Linked list intersection & lc142 Circular linked list II
- 修改Jupyter Notebook文件路径
- 考研失败,卷不进大厂,感觉没戏了
- Detailed explanation of transform origin attribute
- 详解机器翻译任务中的BLEU
- Kuboard can't send email and nail alarm problem is solved
- 按键精灵采集学习-矿药采集及跑图
- Abnova membrane protein lipoprotein technology and category display
- Paranoid unqualified company
猜你喜欢

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?

Le Service MySQL manque dans le service informatique

Summary of customer value model (RFM) technology for data analysis

計算機服務中缺失MySQL服務

Apache AB stress test

About some details of final, I have something to say - learn about final CSDN creation clock out from the memory model

Tujia, muniao, meituan... Home stay summer war will start

Redis data migration

The currently released SKU (sales specification) information contains words that are suspected to have nothing to do with baby

机器人技术创新与实践旧版本大纲
随机推荐
About some details of final, I have something to say - learn about final CSDN creation clock out from the memory model
How does an enterprise manage data? Share the experience summary of four aspects of data governance
Introduction to abnova's in vitro mRNA transcription workflow and capping method
Detailed explanation of transform origin attribute
L'étape avancée du pointeur de langage C (haut de gamme) pour l'enroulement des cocons
sql中对集合进行非空校验
Torefs API and toref API
组件的嵌套和拆分
Initial experience of teambiion network disk (Alibaba cloud network disk)
js小练习
Unity C function notes
MySQL service is missing from computer service
RuntimeError: CUDA error: CUBLAS_ STATUS_ ALLOC_ Failed when calling `cublascreate (handle) `problem solving
Bindingexception exception (error reporting) processing
JS small exercise ---- time sharing reminder and greeting, form password display hidden effect, text box focus event, closing advertisement
Mobx knowledge point collection case (quick start)
How to reduce inventory with high concurrency on the Internet
我理想的软件测试人员发展状态
三、高质量编程与性能调优实战 青训营笔记
Precise space-time travel flow regulation system - ultra-high precision positioning system based on UWB