当前位置:网站首页>[dai6] mirror image of JZ27 binary tree
[dai6] mirror image of JZ27 binary tree
2022-07-07 10:29:00 【strawberry47】
subject :
Operate a given binary tree , Transform it into a mirror image of the source binary tree .
Ideas
What returns is a tree , That has to be established TreeNode Well , Thought of two methods :
① Using the queue , Save from right to left node, The order read out in this way is mirrored ; But the answer requires outputting a tree , I don't know how to convert it into a tree
② Using recursion , When children are leaf nodes , Exchange the positions of the left and right nodes ; But I still can't write ,o(╥﹏╥)o
Secretly read the answer , Use the stack to store nodes , Exchange left and right nodes every time you take them out , So I wrote the code according to this idea :
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
#
#
# @param pRoot TreeNode class
# @return TreeNode class
#
class Solution:
def Mirror(self , pRoot: TreeNode) -> TreeNode:
# write code here
if pRoot is None:
return None
q = []
q.append(pRoot)
while len(q):
for i in range(len(q)):
node = q.pop()
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
# swap
tmp = node.left
node.left = node.right
node.right = tmp
return pRoot
answer :
Look at recursion :
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
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
I searched the Internet for a recursive solution :
To solve the problem of recursion is usually a three-step process , Namely :
First step , Define function function -> Flip the tree
The second step , Looking for recursive termination conditions -> It is already a leaf node or It's already empty
The second step , The equivalent relation of recursive function -> Flip node The left and right subtrees of
边栏推荐
- IO模型复习
- Fiddler break point
- 施努卡:机器视觉定位技术 机器视觉定位原理
- Vs code specifies the extension installation location
- Slurm资源管理与作业调度系统安装配置
- 宁愿把简单的问题说一百遍,也不把复杂的问题做一遍
- Serial communication relay Modbus communication host computer debugging software tool project development case
- 【acwing】786. 第k个数
- . Net configuration system
- 嵌入式工程师如何提高工作效率
猜你喜欢
Postman interface test VI
【二开】【JeecgBoot】修改分页参数
对存储过程进行加密和解密(SQL 2008/SQL 2012)
ArcGIS operation: converting DWG data to SHP data
对word2vec的一些浅层理解
[STM32] solution to the problem that SWD cannot recognize devices after STM32 burning program
[sword finger offer] 42 Stack push in and pop-up sequence
IO模型复习
IO model review
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
随机推荐
IDA中常见快捷键
关于hzero-resource报错(groovy.lang.MissingPropertyException: No such property: weight for class)
浅谈日志中的返回格式封装格式处理,异常处理
IPv4套接字地址结构
Vs code specifies the extension installation location
求方程ax^2+bx+c=0的根(C语言)
原型与原型链
0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)
Postman interface test VI
Postman interface test III
Appx代码签名指南
C logging method
XML configuration file parsing and modeling
Kotlin realizes wechat interface switching (fragment exercise)
Prototype and prototype chain
P1223 排队接水/1319:【例6.1】排队接水
对word2vec的一些浅层理解
2022.7.3DAY595
Appx代碼簽名指南
深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!