当前位置:网站首页>Leetcode sword finger offer 27. image of binary tree
Leetcode sword finger offer 27. image of binary tree
2022-07-25 11:29:00 【kt1776133839】
Title Description :
Please complete a function , Enter a binary tree , This function outputs its image .
For example, the input :
4
/ \
2 7
/ \ / \
1 3 6 9
airplay mirroring :
4
/ \
7 2
/ \ / \
9 6 3 1
Examples :
Example 1:
Input :root = [4,2,7,1,3,6,9]
Output :[4,7,2,9,6,3,1]
Limit :
0 <= Number of nodes <= 1000
Their thinking :
Binary tree image definition :
For any node in a binary tree root , Set its left / The right child nodes are left,right; The corresponding in the image of the binary tree root node , To the left / The right child nodes are right,left .

recursive
This is a classic binary tree problem . obviously , We start at the root node , Recursively traverse the tree , And first flip the leaf node to get the image . If the node currently traversed root\textit{root}root Both the left and right subtrees of have been flipped to get a mirror image , Then we just need to exchange the positions of the two subtrees , You can get root\textit{root}root It is a mirror image of the entire subtree of the root node .
Java Program :
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode left = mirrorTree(root.left);
TreeNode right = mirrorTree(root.right);
root.left = right;
root.right = left;
return root;
}
}
边栏推荐
- Shell fourth day homework
- HCIP (01)
- SQL语言(五)
- 【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源
- Learn NLP with Transformer (Chapter 2)
- A troubleshooting record of DirectShow playback problems
- feign客户端请求之LoadBalancerLifecycle生命周期
- SQL语言(三)
- LVS load balancing lvs-dr builds Web Clusters and LVS combines with kept to build highly available Web Clusters
- Smart cloud IOT platform STM32 esp8266-01s simple wireless light control
猜你喜欢

HCIP (01)

MySQL | GROUP_ The concat function concatenates the values of a column with commas

Learn NLP with Transformer (Chapter 7)

机智云物联网平台 STM32 ESP8266-01S 简单无线控灯

Reinforcement Learning 强化学习(四)

Implementation of recommendation system collaborative filtering in spark

The B2B2C multi merchant system has rich functions and is very easy to open!!!

NowCoderTOP1-6——持续更新ing

Learn NLP with Transformer (Chapter 6)

让运动自然发生,FITURE打造全新生活方式
随机推荐
Signal integrity (SI) power integrity (PI) learning notes (XXXIII) 102 general design rules to minimize signal integrity problems
SQL language (III)
The B2B2C multi merchant system has rich functions and is very easy to open!!!
[动态规划] 70. 爬楼梯
Hcip experiment (02)
SQL语言(三)
Definition of information entropy
BeautifulSoup的一些用法
复习背诵整理版
活动报名 | 玩转 Kubernetes 容器服务提高班正式开营!
Nowcodertop7-11 - continuous updating
SQL language (I)
HCIA experiment (06)
ArcMap无法启动解决方法
学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”
将字符串转换为数字
Why should the hashcode () method be rewritten when rewriting the equals () method
LVS load balancing lvs-dr builds Web Clusters and LVS combines with kept to build highly available Web Clusters
Esp32c3 based on the example tutorial of esp32 Rainmaker development under Arduino framework
Learn NLP with Transformer (Chapter 2)