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

边栏推荐
- Word箭头上面怎么打字
- Don't use array.length-1 to get the last item of the array
- Cholesterol-PEG-Thiol,CLS-PEG-SH,胆固醇-聚乙二醇-巯基用于改善溶解度
- 没有 accept,我可以建立 TCP 连接吗?
- Reverse Analysis of Unknown Cryptographic Protocol Based on Network Data Flow
- Next second data: the transformation of the modern data stack brought about by the integration of lake and warehouse has begun
- What are the benefits of enterprise data integration?How do different industries solve the problem of data access?
- Referenced file contains errors 完美解决方法
- 定投的基金
- PCL学习之滤波Filtering
猜你喜欢
随机推荐
Don't tell me to play, I'm taking the PMP exam: what you need to know about choosing an institution for the PMP exam
继承关系下构造方法的访问特点
Ali Ermi: Without accept, can a TCP connection be established?
下秒数据:湖仓一体带来的现代数据堆栈变革开始了
Definition and Basic Operations of Sequence Tables
LeetCode Daily Question (321. Create Maximum Number)
hdu 1503 Advanced Fruits(最长公共子序列的应用)
【stack】【queue】【priority_queue】【deque】详解
tki-tree 树组件控制默认展开第几层数据
How to deal with keys when Redis is large?
How are data integration APIs key to enterprise digital transformation?
SqlServer 2016 备份和还原
PMP每日一练 | 考试不迷路-8.9(包含敏捷+多选)
字节一面:TCP 和 UDP 可以使用同一个端口吗?
线性表的定义和基本操作
[corctf 2022] 部分
fixed investment fund
基于模糊PID控制器的水温控制系统仿真
Visual studio 2022 debugging skills introduction
Endpoint mode for NetCore routing









