当前位置:网站首页>[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
边栏推荐
猜你喜欢

SolidWorks工程图中添加中心线和中心符号线的办法

leetcode-304:二维区域和检索 - 矩阵不可变

【acwing】786. 第k个数

LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件

SQLyog数据库怎么取消自动保存更改

Some properties of leetcode139 Yang Hui triangle

Leetcode exercise - 113 Path sum II

【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码

【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法

Programming features of ISP, IAP, ICP, JTAG and SWD
随机推荐
Multisim--软件相关使用技巧
小程序跳转H5,配置业务域名经验教程
.NET配置系统
UnityWebRequest基础使用之下载文本、图片、AB包
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
基于gis三维可视化技术的智慧城市建设
对存储过程进行加密和解密(SQL 2008/SQL 2012)
A small problem of bit field and symbol expansion
P2788 数学1(math1)- 加减算式
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
IO model review
Download Text, pictures and ab packages used by unitywebrequest Foundation
移动端通过设置rem使页面内容及字体大小自动调整
[email protected]能帮助我们快速拿到日志对象
学习记录——高精度加法和乘法
Use of JSON extractor originals in JMeter
ThreadLocal会用可不够
Adb 实用命令(网络包、日志、调优相关)
MCU is the most popular science (ten thousand words summary, worth collecting)