当前位置:网站首页>Mirror image of sword finger offer binary tree
Mirror image of sword finger offer binary tree
2022-06-24 23:34:00 【ciwei24456】
Title Description :
Operate a given binary tree , Transform it into a mirror image of the source binary tree .
such as :
Source binary tree
Mirror binary tree
Their thinking :
According to the definition of binary tree image , Consider recursive traversal (dfs) Binary tree , Swap the left side of each node / The right child node , The image of binary tree can be generated .
The problem solving steps :
1、 Special judgement : If pRoot It's empty , Returns an empty
2、 Exchange left and right subtrees
3、 hold pRoot Put the left subtree of Mirror Mirror in
4、 hold pRoot The right subtree of Mirror Mirror in
5、 Return root node root
Python Code implementation :
class Solution:
def Mirror(self , pRoot ):
# write code here
if not pRoot:
return pRoot
# Left and right subtrees exchange
pRoot.left, pRoot.right = pRoot.right, pRoot.left
# Recursive left and right subtrees
self.Mirror(pRoot.left)
self.Mirror(pRoot.right)
return pRoot
边栏推荐
- Enterprise data leakage prevention solution sharing
- 376. 機器任務
- 都2022年了,你还不了解什么是性能测试?
- Idea creation module prompt already exists
- Helix distance of point
- R language dplyr package group_ By function and summarize_ The at function calculates the dataframe to calculate the number of counts and the mean value of different groups (summary data by category v
- throttle-debounce.js:一个小型的防抖节流函数库
- Websocket learning
- Leetcode topic [array] -39- combined sum
- R语言dplyr包group_by函数和summarise_at函数计算dataframe计算不同分组的计数个数和均值(Summarise Data by Categorical Variable)
猜你喜欢
Understanding openstack network
Actipro WPF Controls 2022.1.2
2021-2022 China's financial digitalization "new" insight Industry Research Report
一文理解OpenStack网络
Huawei machine learning service speech recognition function enables applications to paint "sound" and color
STM32CubeIDE SWV功能使用方法
Design and practice of vivo server monitoring architecture
7-7 数字三角形
[JS] - [array, stack, queue, linked list basics] - Notes
Why is it that the "Zhongtai" that was originally eaten by civil engineering is no longer fragrant?
随机推荐
7-3 最大子段和
Use of types, values, namespaces, combinations, etc. in typescript
Mousse shares listed on Shenzhen Stock Exchange: becoming popular by mattress and "foreign old man", with a market value of 22.4 billion yuan
HarmonyOS访问数据库实例(3)--用ORM Bee测下HarmonyOS到底有多牛
7-9 寻宝路线
QT to place the form in the lower right corner of the desktop
Adding, deleting, querying and modifying MySQL tables
7-5 maximal submatrix sum problem
Whereabouts computer desktop small arrow
Morris traversal
Unveiling the secrets of the Winter Olympics | smartbi's partners supported the "front and back" of the Beijing Winter Olympics
第六章 网络学习相关技巧5(超参数验证)
golang convert json string to map
go 语言指针,值引用和指针引用
Volcano becomes spark default batch scheduler
当初吃土建起来的“中台”,现在为啥不香了?
372. 棋盘覆盖
Tomorrow is the PMP Exam (June 25). Have you understood all this?
Fibonacci
376. 机器任务