当前位置:网站首页>小黑leetcode之旅:590. N 叉树的后序遍历
小黑leetcode之旅:590. N 叉树的后序遍历
2022-07-22 19:35:00 【小黑无敌】
1.非递归法
""" # Definition for a Node. class Node: def __init__(self, val=None, children=None): self.val = val self.children = children """
class Solution:
def postorder(self, root: 'Node') -> List[int]:
if not root:
return []
stack = [root]
display = []
while stack:
top = stack[-1]
if not top.children or top.children[0].val in display:
display.append(top.val)
stack.pop()
else:
stack.extend(reversed(top.children))
return display

2.递归法
""" # Definition for a Node. class Node: def __init__(self, val=None, children=None): self.val = val self.children = children """
class Solution:
def postorder(self, root: 'Node') -> List[int]:
display = []
if not root:
return []
def after_node(node):
for child in node.children:
after_node(child)
display.append(node.val)
after_node(root)
return display

3.先根遍历逆转法
""" # Definition for a Node. class Node: def __init__(self, val=None, children=None): self.val = val self.children = children """
class Solution:
def postorder(self, root: 'Node') -> List[int]:
display = []
if not root:
return display
stack = [root]
while stack:
top = stack.pop()
display.append(top.val)
stack.extend(top.children)
return reversed(display)

边栏推荐
- 《STL仿函数》priority_queue模拟实现
- Gom引擎Key.lic配套的X-FKGOM授权启动
- 电脑不能截屏怎么办?电脑的快捷截屏键无法使用的解决办法
- 小米活期宝和余额宝哪个好?小米活期宝与阿里余额宝区别详细对比介绍
- 弱网优化(SharePreference)原理+实战剖析
- 实现国标GB28181流媒体服务解决方案
- PHP prevents or detects repeated post submissions when the page is refreshed
- TCP waves four times
- 无法打开代理服务器提示代理服务器没有设置为完全访问该怎么办?
- mysql之外键操作_级联操作
猜你喜欢

二级倒立摆系统的稳定控制与仿真(Matlab/Simulink)

Priority of STL functor_ Queue simulation implementation

小红书携手HMS Core,畅玩高清视界,种草美好生活

引擎提示Alias HeroDB跟游戏引擎启动异常怎么解决?

HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态

Realize OPC UA publish/subscribe single send

Jupyternotebook runs to the specified line

华为昇腾比赛资料

At the forefront of the times, Huawei aims at the wind and sea of digital finance

事件抽取文献整理(2019)
随机推荐
Mktdt03 of Shanghai stock exchange quotation document analysis
An in-depth explanation of CAS is necessary for interview practice
Priority of STL functor_ Queue simulation implementation
ABAP ALV summary
如何将监控画面嵌入微信公众号进行直播
Pay attention to the pitfalls of using enumeration in MySQL!
固态硬盘复制文件只有几十kb怎么办? 电脑安装硬盘后复制速度慢的解决办法
为什么我的百度账号登不上去了?百度账号无法登录现象的解决方法介绍
如何让屏幕上的字显示更大(让大屏幕看文字更舒服的设置方法)
CloudWeGo 在飞书管理后台平台化设计实践
Interviewer: how to solve the problem of a large number of requests for data that does not exist in redis, which affects the database?
TCP四次挥手
LUR缓存算法
电脑如何快速关机 电脑关机命令分享
电商项目如何解决线上优惠券超发(排错+解决方案)(荣耀典藏)
How to embed the monitoring image into wechat official account for live broadcast
电脑下载的软件不在桌面显示怎么办 解决安装后的软件不在桌面问题
电脑分区时出现'磁盘上没有足够的空间完成此操作'的解决办法
ABAP ALV步骤
百度贴吧签名档怎么弄?百度贴吧签名档设置使用方法图文介绍