当前位置:网站首页>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;
}
}
边栏推荐
- HDD Hangzhou station full experience
- Learn NLP with Transformer (Chapter 3)
- MySQL master-slave replication and read-write separation
- Nowcodertop12-16 - continuous updating
- From the perspective of open source, analyze the architecture design of SAP classic ERP that will not change in 30 years
- 从宏观到微观 零基础 详解bert
- Hcip experiment (01)
- SQL注入 Less18(头部注入+报错注入)
- Learn NLP with Transformer (Chapter 7)
- PostgreSQL stepping on the pit | error: operator does not exist: UUID = character varying
猜你喜欢

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

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

Reinforcement Learning 强化学习(四)

Reinforcement learning (III)

Smart cloud IOT platform STM32 esp8266-01s simple wireless light control

BeautifulSoup的一些用法

SQL language (I)

游戏背包系统,“Inventory Pro插件”,研究学习-----妈妈再也不用担心我不会做背包了(Unity3D)

MySQL advanced statement (I) (there is always someone who will make your life no longer bad)

Stm32cubemx learning record -- installation, configuration and use
随机推荐
Learn PHP -- phpstudy tips mysqld Exe: Error While Setting Value ‘NO_ ENGINE_ Solution of substitution error
Detailed explanation of the implementation method of DNS separation and resolution
学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”
Dataframe print ellipsis problem
Learn NLP with Transformer (Chapter 8)
[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training
Only know that the preform is used to generate objects? See how I use unity to generate UI prefabs
Reptile foundation I
shell- 第七章练习
新能源销冠宏光MINIEV,有着怎样的产品力?
web移动端:touchmove实现局部滚动
MySQL | GROUP_CONCAT函数,将某一列的值用逗号拼接
HCIA experiment (06)
HCIA experiment (08)
Learn NLP with Transformer (Chapter 5)
学习路之PHP--Phpstudy 提示 Mysqld.Exe: Error While Setting Value ‘NO_ENGINE_SUBSTITUTION 错误的解决办法
Ue4.26 source code version black screen problem of client operation when learning Wan independent server
HCIA experiment (07) comprehensive experiment
Stm32cubemx learning record -- installation, configuration and use
Database design - Simplified dictionary table [easy to understand]