当前位置:网站首页>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 .
边栏推荐
- 考研失败,卷不进大厂,感觉没戏了
- Détailler le bleu dans les tâches de traduction automatique
- Bi she - college student part-time platform system based on SSM
- mips uclibc 交叉编译ffmpeg,支持 G711A 编解码
- 修改Jupyter Notebook文件路径
- Music | cat and mouse -- classic not only plot
- Implementation of AVL tree
- Torefs API and toref API
- js小练习
- Jesd204b clock network
猜你喜欢

面试官:你都了解哪些开发模型?

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

BGP experiment (1)

Pass parent component to child component: props

Deep learning Flower Book + machine learning watermelon book electronic version I found

考研失败,卷不进大厂,感觉没戏了

$refs: get the element object or sub component instance in the component:

外包干了四年,废了...

弹性布局(二)

Cloud backup project
随机推荐
Bi she - college student part-time platform system based on SSM
组件的嵌套和拆分
Dynamics CRM server deployment - restore database prompt: the database is in use
Deep learning Flower Book + machine learning watermelon book electronic version I found
Software acceptance test
1、 Go knowledge check and remedy + practical course notes youth training camp notes
Project practice five fitting straight lines to obtain the center line
Modify the jupyter notebook file path
Build personal website based on flask
Flexible layout (I)
Tujia, muniao, meituan... Home stay summer war will start
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
抽丝剥茧C语言(高阶)数据的储存+练习
考研失败,卷不进大厂,感觉没戏了
mips uclibc 交叉编译ffmpeg,支持 G711A 编解码
抽絲剝繭C語言(高階)數據的儲存+練習
Databinding exception of kotlin
Asynchronous components and suspend (in real development)
Differences between H5 architecture and native architecture
RuntimeError: CUDA error: CUBLAS_ STATUS_ ALLOC_ Failed when calling `cublascreate (handle) `problem solving