当前位置:网站首页>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;
}
}
边栏推荐
- My colleague looked at my code and exclaimed: how can I use a singleton in unity
- NowCoderTOP7-11——持续更新ing
- LVS负载均衡之LVS-NAT与LVS-DR模式原理详解
- DNS分离解析的实现方法详解
- Only know that the preform is used to generate objects? See how I use unity to generate UI prefabs
- Learn NLP with Transformer (Chapter 2)
- Dataframe print 省略号问题
- Detailed explanation of zero basis from macro to micro Bert
- Let sports happen naturally, and fire creates a new lifestyle
- Learn NLP with Transformer (Chapter 5)
猜你喜欢

Ue4.26 source code version black screen problem of client operation when learning Wan independent server

Nowcodertop1-6 - continuous updating

黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了。

A troubleshooting record of DirectShow playback problems
Definition of information entropy

Learn NLP with Transformer (Chapter 7)

Implementation of recommendation system collaborative filtering in spark

SQL language (III)

SQL注入 Less23(过滤注释符)

工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖
随机推荐
史上最全的立创元器件封装库导入AD详细教程(一直白嫖一直爽)
Common web attacks and defense
Detailed explanation of zero basis from macro to micro Bert
Txt to CSV file, blank lines appear every other line
shell-第六章练习
NowCoderTOP1-6——持续更新ing
JS convert pseudo array to array
LVS load balancing lvs-nat building Web Cluster
Stm32cubemx learning record -- installation, configuration and use
Dataframe print ellipsis problem
SQL语言(二)
Mlx90640 infrared thermal imager temperature measurement module development notes (V)
玩游戏想记录一下自己超神的瞬间?那么就来看一下如何使用Unity截图吧
[flask advanced] deeply understand the application context and request context of flask from the source code
Guys, flick CDC table API, Mysql to MySQL, an application that can
推荐系统-协同过滤在Spark中的实现
Shell 脚本参数传递时有 \r 换行符问题
LVS负载均衡之LVS-NAT搭建Web群集
[information system project manager] thought map series essence summary
SQL语言(三)