当前位置:网站首页>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 .
边栏推荐
- Abnova membrane protein lipoprotein technology and category display
- 按键精灵采集学习-矿药采集及跑图
- Model application of time series analysis - stock price prediction
- Composition API premise
- Pass parent component to child component: props
- Stockage et pratique des données en langage C (haut niveau)
- Lm11 reconstruction of K-line and construction of timing trading strategy
- Le Service MySQL manque dans le service informatique
- 2、 Concurrent and test notes youth training camp notes
- Torefs API and toref API
猜你喜欢
Pass child component to parent component
Detailed explanation of neo4j installation process
Kuboard无法发送邮件和钉钉告警问题解决
English translation is too difficult? I wrote two translation scripts with crawler in a rage
Initial experience of teambiion network disk (Alibaba cloud network disk)
【leetcode】1020. Number of enclaves
Bindingexception exception (error reporting) processing
Lm11 reconstruction of K-line and construction of timing trading strategy
Deep learning Flower Book + machine learning watermelon book electronic version I found
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
随机推荐
4、 High performance go language release optimization and landing practice youth training camp notes
外包幹了三年,廢了...
Tujia, muniao, meituan... Home stay summer war will start
LC interview question 02.07 Linked list intersection & lc142 Circular linked list II
Le Service MySQL manque dans le service informatique
关于二进制无法精确表示小数
Blue Bridge Cup Birthday candles (violence)
Cloud backup project
FPGA course: application scenario of jesd204b (dry goods sharing)
Sqlmap tutorial (IV) practical skills three: bypass the firewall
English translation is too difficult? I wrote two translation scripts with crawler in a rage
【云原生】内存数据库如何发挥内存优势
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?
RuntimeError: CUDA error: CUBLAS_ STATUS_ ALLOC_ Failed when calling `cublascreate (handle) `problem solving
Paranoid unqualified company
JS small exercise
A concurrent rule verification implementation
Torefs API and toref API
Pass parent component to child component: props
Chinese and English instructions prosci LAG-3 recombinant protein