当前位置:网站首页>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 .
边栏推荐
- BGP experiment (1)
- Composition API premise
- 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
- Stack Title: nesting depth of valid parentheses
- ROS2规划系统plansys2简单的例子
- Pass parent component to child component: props
- [semantic segmentation] - multi-scale attention
- Tumor immunotherapy research prosci Lag3 antibody solution
- Advanced level of C language (high level) pointer
- Simple example of ros2 planning system plansys2
猜你喜欢

English translation is too difficult? I wrote two translation scripts with crawler in a rage

sql中对集合进行非空校验

Asynchronous components and suspend (in real development)

毕设-基于SSM大学生兼职平台系统
![[Linux] process control and parent-child processes](/img/4c/89f87ee97f0f8e9033b9f0ef46a80d.png)
[Linux] process control and parent-child processes

关于二进制无法精确表示小数

抽絲剝繭C語言(高階)數據的儲存+練習

Talk about seven ways to realize asynchronous programming

Abnova immunohistochemical service solution

About binary cannot express decimals accurately
随机推荐
Invalid table alias or column reference`xxx`
Project practice five fitting straight lines to obtain the center line
About binary cannot express decimals accurately
Build personal website based on flask
Select the product attribute pop-up box to pop up the animation effect from the bottom
Flexible layout (II)
L'externalisation a duré trois ans.
Dynamics CRM server deployment - restore database prompt: the database is in use
Abnova circulating tumor DNA whole blood isolation, genomic DNA extraction and analysis
Paranoid unqualified company
基于Flask搭建个人网站
Advanced practice of C language (high level) pointer
Differences between H5 architecture and native architecture
SQLMAP使用教程(四)实战技巧三之绕过防火墙
URP - shaders and materials - simple lit
[cloud native] how to give full play to memory advantage of memory database
Lm11 reconstruction of K-line and construction of timing trading strategy
Databinding exception of kotlin
Pass child component to parent component
聊聊异步编程的 7 种实现方式