当前位置:网站首页>小黑leetcode之旅:94. 二叉树的中序遍历(补充Morris 中序遍历)
小黑leetcode之旅:94. 二叉树的中序遍历(补充Morris 中序遍历)
2022-08-09 21:35:00 【小黑无敌】
小黑学习后实现
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
arr = []
while root:
# 指针指向左孩子
pre = root.left
# 左孩子不为空(左孩子"最右"结点连接root,root变为左孩子)
if pre:
# 寻找最右孩子
while pre.right and pre.right != root:
pre = pre.right
# 最右孩子指向root,则打印root,随后root为root.right
if pre.right:
#pre.right = None
arr.append(root.val)
root = root.right
# 其为空,将最右孩子的右侧指针指向root,然后将root = root.left
else:
pre.right = root
root = root.left
# 左孩子为空(打印,root变为右孩子)
else:
arr.append(root.val)
root = root.right
return arr

边栏推荐
猜你喜欢

matlab 神经网络 ANN 分类

Word第一页不要页眉怎么设置?设置Word首页不要页眉方法教程

Number of daffodils within a thousand

DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine reduce the immunogenicity of peptides

PCL学习之滤波Filtering

Access control knowledge

Problems with compiling SIP with QGIS

微软Excel表格点击单元格行和列都显示颜色怎么弄?聚光灯效果设置

Lyapp exponents and bifurcation diagrams for fractional chaotic systems

Wps下划线怎么弄?Wps添加下划线的最全方法
随机推荐
What are the benefits of enterprise data integration?How do different industries solve the problem of data access?
Cholesterol-PEG-Thiol,CLS-PEG-SH,胆固醇-聚乙二醇-巯基用于改善溶解度
SqlServer 2016 备份和还原
获取数组最后一项别再用array.length-1了
UE4_定序器控制蓝图对象
蓝牙模块有哪些种类?BLE低功耗蓝牙模块有什么特点?
自监督学习 —— MoCo v2
【stack】【queue】【priority_queue】【deque】详解
windos安装Mysql8.0,及解决重新登录异常问题 ERROR 1045 (28000)
Unity2D_背景粒子效果
hdu 3341 Lost's revenge(dp+Ac自动机)
SqlServer 2016 安装相关问题
安科瑞无线物联网智能电表ADW300指导性技术要求-Susie 周
一种基于连接和安全熵的网络空间整体安全认识和方法
What to do if Windows 11 can't find Internet Explorer
什么是IDE(集成开发环境)?
Interviewer: How to deal with Redis big key?
Word第一页不要页眉怎么设置?设置Word首页不要页眉方法教程
Two methods of implementing inverted strings in C language
matlab 神经网络 ANN 分类