当前位置:网站首页>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;
}
}
边栏推荐
- 使用Three.js实现炫酷的赛博朋克风格3D数字地球大屏
- 推荐系统-协同过滤在Spark中的实现
- [dynamic planning] 70. Climbing stairs
- Nowcodertop12-16 - continuous updating
- Detailed explanation of zero basis from macro to micro Bert
- MLX90640 红外热成像仪测温模块开发笔记(五)
- Signal integrity (SI) power integrity (PI) learning notes (XXXIV) 100 rules of thumb for estimating signal integrity effects
- 复习背诵整理版
- Nowcodertop1-6 - continuous updating
- HCIP (01)
猜你喜欢

Let sports happen naturally, and fire creates a new lifestyle
Learn NLP with Transformer (Chapter 4)

Understanding: idea uses Scala to write wordcount programs and generate jar packages

The most detailed MySQL index analysis (mind map is attached at the end of the article)

Hcip experiment (02)

活动报名 | 玩转 Kubernetes 容器服务提高班正式开营!

Learn PHP -- phpstudy tips mysqld Exe: Error While Setting Value ‘NO_ ENGINE_ Solution of substitution error

HCIP(11)

Reinforcement Learning 强化学习(四)

一篇看懂:IDEA 使用scala 编写wordcount程序 并生成jar包 实测
随机推荐
如何判断静态代码质量分析工具的性能?这五大因素必须考虑
leetcode 剑指 Offer 27. 二叉树的镜像
活动报名 | 玩转 Kubernetes 容器服务提高班正式开营!
Learn NLP with Transformer (Chapter 6)
Dataframe print ellipsis problem
ESP8266 使用 DRV8833驱动板驱动N20电机
Esp8266 uses drv8833 drive board to drive N20 motor
Detailed explanation of zero basis from macro to micro Bert
Common web attacks and defense
Syncronized lock upgrade process
BGP federal experiment
Nowcodertop12-16 - continuous updating
tensorflow入门
数据库设计-简化字典表[通俗易懂]
为什么重写equals()方法必须要重写hashCode()方法
My colleague looked at my code and exclaimed: how can I use a singleton in unity
A troubleshooting record of DirectShow playback problems
Compressed list ziplist of redis
Motivation of enterprises to practice open source
推荐系统-协同过滤在Spark中的实现