当前位置:网站首页>[牛客网刷题 Day6] JZ27 二叉树的镜像
[牛客网刷题 Day6] JZ27 二叉树的镜像
2022-07-07 08:14:00 【strawberry47】
题目:
操作给定的二叉树,将其变换为源二叉树的镜像。
思路
返回的是一棵树,那得建立TreeNode吧,想到了两种方法:
① 使用队列,从右往左存node,这样读出来的顺序就是镜像的;可是答案要求输出一颗树,我不知道怎么转换成树
② 使用递归,当孩子为叶节点时,交换左右节点的位置;可是还是写不来,o(╥﹏╥)o
偷偷看了答案,用堆栈存储节点,每次取出来就交换左右节点,于是照着这个思路写了一下代码:
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pRoot TreeNode类
# @return TreeNode类
#
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
答案:
看了看递归:
解题步骤:
1、特判:如果pRoot为空,返回空
2、交换左右子树
3、把pRoot的左子树放到Mirror中镜像一下
4、把pRoot的右子树放到Mirror中镜像一下
5、返回根节点root
class Solution:
def Mirror(self , pRoot ):
# write code here
if not pRoot:
return pRoot
# 左右子树交换
pRoot.left, pRoot.right = pRoot.right, pRoot.left
# 递归左右子树
self.Mirror(pRoot.left)
self.Mirror(pRoot.right)
return pRoot
网上搜了一下递归的解法:
解决递归问题一般就三步曲,分别是:
第一步,定义函数功能 -> 翻转树
第二步,寻找递归终止条件 -> 已经是叶节点了or已经为空了
第二步,递推函数的等价关系式 -> 翻转node的左子树和右子树
边栏推荐
- Why is the reflection efficiency low?
- STM32 Basics - memory mapping
- Talking about the return format in the log, encapsulation format handling, exception handling
- 每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
- Some thoughts on the testing work in the process of R & D
- Study summary of postgraduate entrance examination in October
- Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
- Es classes and objects, prototypes
- The method of word automatically generating directory
- Learning records - high precision addition and multiplication
猜你喜欢
P1223 排队接水/1319:【例6.1】排队接水
【acwing】786. Number k
High number_ Chapter 1 space analytic geometry and vector algebra_ Quantity product of vectors
php \n 换行无法输出
Postman interface test VII
Postman interface test II
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
ORM -- grouping query, aggregation query, query set queryset object properties
XML配置文件解析与建模
搭建物联网硬件通信技术几种方案
随机推荐
Interface test
Or in SQL, what scenarios will lead to full table scanning
P1223 排队接水/1319:【例6.1】排队接水
大整数类实现阶乘
根据设备信息进行页面跳转至移动端页面或者PC端页面
移动端通过设置rem使页面内容及字体大小自动调整
A wave of open source notebooks is coming
关于hzero-resource报错(groovy.lang.MissingPropertyException: No such property: weight for class)
ArcGIS operation: batch modify attribute table
Postman interface test III
Study summary of postgraduate entrance examination in July
[email protected] can help us get the log object quickly
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
Methods of adding centerlines and centerlines in SolidWorks drawings
The landing practice of ByteDance kitex in SEMA e-commerce scene
ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
Programming features of ISP, IAP, ICP, JTAG and SWD
求方程ax^2+bx+c=0的根(C语言)
Use of JSON extractor originals in JMeter
The request object parses the request body and request header parameters