当前位置:网站首页>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 .
边栏推荐
- 【云原生】内存数据库如何发挥内存优势
- Communication between non parent and child components
- Tumor immunotherapy research prosci Lag3 antibody solution
- Databinding exception of kotlin
- MIPS uclibc cross compile ffmpeg, support g711a encoding and decoding
- Hidden Markov model (HMM) learning notes
- js小练习
- Select the product attribute pop-up box to pop up the animation effect from the bottom
- Robot technology innovation and practice old version outline
- Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.10 tabbar component (I) how to open and use the default tabbar comp
猜你喜欢
Advanced level of C language (high level) pointer
Tujia, muniao, meituan... Home stay summer war will start
Convolutional neural network -- understanding of pooling
About binary cannot express decimals accurately
考研失败,卷不进大厂,感觉没戏了
Pass parent component to child component: props
Asynchronous components and suspend (in real development)
计算机服务中缺失MySQL服务
freeswitch拨打分机号源代码跟踪
Detailed explanation of neo4j installation process
随机推荐
SQLMAP使用教程(四)实战技巧三之绕过防火墙
Explain Bleu in machine translation task in detail
4、 High performance go language release optimization and landing practice youth training camp notes
Tujia, muniao, meituan... Home stay summer war will start
选择商品属性弹框从底部弹出动画效果
Communication of components
Tumor immunotherapy research prosci Lag3 antibody solution
Implementation of AVL tree
Example of Pushlet using handle of Pushlet
After 95, the CV engineer posted the payroll and made up this. It's really fragrant
Bindingexception exception (error reporting) processing
Cloud backup project
JS small exercise ---- time sharing reminder and greeting, form password display hidden effect, text box focus event, closing advertisement
Stockage et pratique des données en langage C (haut niveau)
按键精灵采集学习-矿药采集及跑图
leetcode 509. Fibonacci number
Precise space-time travel flow regulation system - ultra-high precision positioning system based on UWB
Sqlmap tutorial (IV) practical skills three: bypass the firewall
Abnova circulating tumor DNA whole blood isolation, genomic DNA extraction and analysis
毕设-基于SSM大学生兼职平台系统