当前位置:网站首页>小黑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
边栏推荐
- Acrel5000web能耗系统在某学院的应用-Susie 周
- Number of daffodils within a thousand
- Simulation of Water Temperature Control System Based on Fuzzy PID Controller
- Win11搜索不到文件的解决方法
- Two methods of implementing inverted strings in C language
- Word第一页空白页怎么删除?删除Word第一页空白页方法教程
- 【深度学习】pix2pix GAN理论及代码实现
- Characteristics and Development Prospects of Korea's Cyber Security System
- How are data integration APIs key to enterprise digital transformation?
- Install Mysql8.0 on windos, and solve the problem of re-login exception ERROR 1045 (28000)
猜你喜欢
浅谈Numpy中的shape、reshape函数的区别
Can I make a TCP connection without accept?
Don't use array.length-1 to get the last item of the array
Lyapp exponents and bifurcation diagrams for fractional chaotic systems
LoRa Basics无线通信技术和应用案例详解
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰二氧化硅颗粒用
线段相交的应用
DSPE-PEG-Azide,DSPE-PEG-N3,磷脂-聚乙二醇-叠氮可和DBCO直接反应
Application of Acrel5000web Energy Consumption System in a College-Susie Week
[Graphic and textual] How to reinstall Win7 system
随机推荐
CMake 安装升级更高版本
Word怎么制作一张标准的答题卡?
Excel如何打出正负号?Excel打出正负号的方法
[Essay] To the friends of the 19th issue
hdu 3341 Lost's revenge(dp+Ac自动机)
倍福CX5120实现温度控制例程详细解析
10个 Istio 流量管理 最常用的例子,你知道几个?
什么是IDE(集成开发环境)?
DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine reduce the immunogenicity of peptides
How are data integration APIs key to enterprise digital transformation?
一种基于连接和安全熵的网络空间整体安全认识和方法
微软Excel表格点击单元格行和列都显示颜色怎么弄?聚光灯效果设置
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified silica particles
DSPE-PEG-Azide, DSPE-PEG-N3, phospholipid-polyethylene glycol-azide can react directly with DBCO
matlab 神经网络 ANN 分类
线段相交的应用
MySQL Notes-06 Basic SQL Operations
C语言中的文件是什么?
Optimization of SQL Statements and Indexes
Overview of Security Analysis Technology for Smart Home Devices