当前位置:网站首页>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 .
边栏推荐
- Interviewer: what development models do you know?
- [Linux] process control and parent-child processes
- Bindingexception exception (error reporting) processing
- At the age of 20, I got the ByteDance offer on four sides, and I still can't believe it
- LC interview question 02.07 Linked list intersection & lc142 Circular linked list II
- [semantic segmentation] - multi-scale attention
- 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
- Stockage et pratique des données en langage C (haut niveau)
- ViewModelProvider. Of obsolete solution
- How do I get the last part of a string- How to get the last part of a string?
猜你喜欢
随机推荐
[cloud native] how to give full play to memory advantage of memory database
Fast quantitative, abbkine protein quantitative kit BCA method is coming!
选择商品属性弹框从底部弹出动画效果
1090: integer power (multi instance test)
外包幹了三年,廢了...
软件验收测试
深度学习花书+机器学习西瓜书电子版我找到了
SQLMAP使用教程(四)实战技巧三之绕过防火墙
PostgreSQL source code (59) analysis of transaction ID allocation and overflow judgment methods
Abnova circulating tumor DNA whole blood isolation, genomic DNA extraction and analysis
MIPS uclibc cross compile ffmpeg, support g711a encoding and decoding
Mobx knowledge point collection case (quick start)
Communication of components
C language (high-level) data storage + Practice
Implementing data dictionary with JSP custom tag
Advanced level of C language (high level) pointer
我理想的软件测试人员发展状态
按键精灵采集学习-矿药采集及跑图
How to * * labelimg
JS small exercise