当前位置:网站首页>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 .
边栏推荐
- 面试官:你都了解哪些开发模型?
- FullGC问题分析及解决办法总结
- [cloud native] how to give full play to memory advantage of memory database
- Freeswitch dials extension number source code tracking
- [explanation of JDBC and internal classes]
- leetcode 509. Fibonacci number
- Détailler le bleu dans les tâches de traduction automatique
- 软件验收测试
- OOM(内存溢出)造成原因及解决方案
- 外包幹了三年,廢了...
猜你喜欢

二、并发、测试笔记 青训营笔记

虚拟机的作用

Talk about seven ways to realize asynchronous programming

Apache AB stress test

四、高性能 Go 语言发行版优化与落地实践 青训营笔记

A concurrent rule verification implementation

L'externalisation a duré trois ans.

我理想的软件测试人员发展状态

Introduction to abnova's in vitro mRNA transcription workflow and capping method

transform-origin属性详解
随机推荐
Simple example of ros2 planning system plansys2
身边35岁程序员如何建立起技术护城河?
四、高性能 Go 语言发行版优化与落地实践 青训营笔记
点亮显示屏的几个重要步骤
Chinese and English instructions prosci LAG-3 recombinant protein
js小练习
抽絲剝繭C語言(高階)數據的儲存+練習
How to reduce inventory with high concurrency on the Internet
URP - shaders and materials - light shader lit
mips uclibc 交叉编译ffmpeg,支持 G711A 编解码
Outlier detection technology of time series data
RuntimeError: CUDA error: CUBLAS_ STATUS_ ALLOC_ Failed when calling `cublascreate (handle) `problem solving
Several important steps to light up the display
My ideal software tester development status
选择商品属性弹框从底部弹出动画效果
Docker compose start redis cluster
07_ Handout on the essence and practical skills of text measurement and geometric transformation
弹性布局(二)
详解机器翻译任务中的BLEU
抽丝剥茧C语言(高阶)数据的储存+练习