当前位置:网站首页>[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
边栏推荐
- 字符串格式化
- Prototype and prototype chain
- Appx代碼簽名指南
- 关于hzero-resource报错(groovy.lang.MissingPropertyException: No such property: weight for class)
- 优雅的 Controller 层代码
- fiddler-AutoResponder
- 柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
- HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
- [email protected] can help us get the log object quickly
- IIC基本知识
猜你喜欢
随机推荐
mysql插入数据创建触发器填充uuid字段值
Hdu-2196 tree DP learning notes
5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
[STM32] solution to the problem that SWD cannot recognize devices after STM32 burning program
Experience sharing of software designers preparing for exams
Guid主键
原型与原型链
IPv4 socket address structure
Fiddler simulates the interface test
JMeter loop controller and CSV data file settings are used together
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
Embedded background - chip
leetcode-304:二维区域和检索 - 矩阵不可变
宁愿把简单的问题说一百遍,也不把复杂的问题做一遍
.NET配置系统
学习记录——高精度加法和乘法
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
How embedded engineers improve work efficiency
IPv4套接字地址结构