当前位置:网站首页>Xiaohei leetcode surfing: 94. Inorder traversal of binary tree
Xiaohei leetcode surfing: 94. Inorder traversal of binary tree
2022-08-04 23:19:00 【little black invincible】
小黑答案:递归
# 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 = []
if not root:
return arr
def mid_root(node):
if node:
mid_root(node.left)
arr.append(node.val)
mid_root(node.right)
mid_root(root)
return arr

小黑答案:非递归
# 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 = []
if not root:
return arr
q = []
node = root
# Push all the children of the root node onto the stack
while node:
q.append(node)
node = node.left
while q:
# 结点出栈
node = q.pop()
# 打印
arr.append(node.val)
# Begin to facilitate the right child
temp = node.right
while temp:
q.append(temp)
temp = temp.left
return arr

迭代法
# 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 = []
if not root:
return arr
q = []
node = root
while node or q:
while node:
q.append(node)
node = node.left
node = q.pop()
arr.append(node.val)
# turn right
node = node.right
return arr

方法三:Morris 中序遍历 未来实现
边栏推荐
- Pytorch分布式训练/多卡/多GPU训练DDP的torch.distributed.launch和torchrun
- I was rejected by the leader for a salary increase, and my anger rose by 9.5K after switching jobs. This is my mental journey
- Acwing3593. 统计单词
- 一点点读懂regulator(二)
- uniapp横向选项卡(水平滚动导航栏)效果demo(整理)
- 仪表板展示 | DataEase看中国:数据呈现中国资本市场
- MySQL基础篇【子查询】
- [QNX Hypervisor 2.2用户手册]10.6 vdev mc146818
- npm基本操作及命令详解
- Ab3d.PowerToys and Ab3d.DXEngine Crack
猜你喜欢

365天深度学习训练营-学习线路

PID控制器改进笔记之七:改进PID控制器之防超调设定

现在学习次世代3D游戏建模还能找到高薪好工作吗

NebulaGraph v3.2.0 Release Note, many optimizations such as the performance of querying the shortest path

Since a new byte of 20K came out, I have seen what the ceiling is

【3D建模制作技巧分享】ZBrush模型如何添加不同材质

Ab3d.PowerToys and Ab3d.DXEngine Crack

地面高度检测/平面提取与检测(Fast Plane Extraction in Organized Point Clouds Using Agglomerative Hierarchical Clu)

未来我们还需要浏览器吗?(feat. 枫言枫语)

话题 | 雾计算和边缘计算有什么区别?
随机推荐
truffle
kernel问题定位手段总结
深度|医疗行业勒索病毒防治解决方案
对“为什么一些程序员很傲慢”的解读
【软件测试】常用ADB命令
PHP(3)
Go 语言快速入门指南:什么是 TSL 安全传输层
基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!
Shell编程之循环语句与函数的使用
The Controller layer code is written like this, concise and elegant!
Day118.尚医通:订单列表、详情、支付
被领导拒绝涨薪申请,跳槽后怒涨9.5K,这是我的心路历程
TypeScript - the use of closure functions
一点点读懂Thremal(二)
ClickHouse 二级索引
Implementing class target method exception using proxy object execution
Pytest学习-Fixture
地面高度检测/平面提取与检测(Fast Plane Extraction in Organized Point Clouds Using Agglomerative Hierarchical Clu)
365天深度学习训练营-学习线路
@Import注解的作用以及如何使用