当前位置:网站首页>小黑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

边栏推荐
猜你喜欢

定投的基金

人人都可以DIY的大玩具,宏光MINIEV GAMEBOY产品力强,出行新装备

基于模糊PID控制器的水温控制系统仿真

MySQL, which is asked on both sides of the byte, almost didn't answer well

Excel如何打出正负号?Excel打出正负号的方法

一千以内的水仙花数

Unity2D_背景粒子效果

Access control knowledge

Simulation of Water Temperature Control System Based on Fuzzy PID Controller

FS4066耐高压1到4节内置MOS的锂电池充电管理芯片
随机推荐
hdu 1333 Smith Numbers(暴力思路)
【随笔】致19期的小伙伴们
线段相交的应用
Definition and Basic Operations of Sequence Tables
Word第一页空白页怎么删除?删除Word第一页空白页方法教程
安科瑞支持以太网通讯、profibus通讯嵌入式电能表APM指导性技术要求-Susie 周
微软Excel表格点击单元格行和列都显示颜色怎么弄?聚光灯效果设置
Overview of Security Analysis Technology for Smart Home Devices
Word怎么制作一张标准的答题卡?
企业数据打通有什么好处?不同行业怎么解决数据打通难题?
【NOI模拟赛】防AK题(生成函数,单位根,Pollard-Rho)
【图文并茂】如何进行Win7系统的重装
痛击面试官 CURD系统也能做出技术含量
Unity2D_背景粒子效果
MySQL跨表、多表更新SQL语句总结
buuctf(探险2)
阿里二面:没有 accept,能建立 TCP 连接吗?
PMP每日一练 | 考试不迷路-8.8(包含敏捷+多选)
How are data integration APIs key to enterprise digital transformation?
顺序表的定义和基本操作