当前位置:网站首页>小黑啃leetcode:589. N 叉树的前序遍历
小黑啃leetcode:589. N 叉树的前序遍历
2022-07-22 19:35:00 【小黑无敌】
递归法
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
class Solution:
def preorder(self, root: 'Node') -> List[int]:
# 定义存储数组
arr = []
def dfs(node):
if node:
arr.append(node.val)
# 依次递归遍历孩子结点
for child in node.children:
dfs(child)
dfs(root)
return arr

非递归法
class Solution:
def preorder(self, root: 'Node') -> List[int]:
if not root:
return []
stack = [root]
display = []
while stack:
node = stack.pop()
display.append(node.val)
stack.extend(node.children[::-1])
return display

边栏推荐
- STL container -list Simulation Implementation (three kinds of reverse iterators)
- 微软测双胞胎工具twinsornot怎么玩?测双胞胎工具twinsornot玩法介绍
- 工行网银助手无法安装怎么办?工行网银助手安装失败问题解决方法
- 什么程序员在35岁依然被公司抢着要?打破程序员“中年危机”
- 龙芯掌门人吐苦水:我们有世界第一性能的CPU,但可惜没人用!
- 微软我们有多像上传不了照片怎么回事?TwinsOrNot照片传不上去的解决方法
- ABAP ALV summary
- 电脑屏幕变大了怎么还原 电脑屏幕显示缩放教程
- LUR缓存算法
- 电脑显示器分辨率多少合适?各尺寸显示器最佳分辨率及不同壁纸的选择介绍
猜你喜欢

threejs+shader绘制常用的图形

TCP waves four times

AWS使用EC2降低DeepRacer的训练成本DeepRacer-for-cloud的实践操作

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

《STL容器篇》-Vector模拟实现

MyCms 自媒体商城 v3.5 发布,新增免费插件

esphome自制红外遥控器接入HA,用来控制灯光,开关等,可使用任意遥控控制

Demo19- (to be updated)

尿酸检测与注意事项

什么程序员在35岁依然被公司抢着要?打破程序员“中年危机”
随机推荐
Gb28181 summary of common problems in the use and secondary development of livegbs streaming media service
thinkphp URL_ Mode =0 specific usage of normal mode
常见的跨域问题
Realize OPC UA publish/subscribe single send
一根网线怎么连接两台电脑传文件 一根网线连接两台电脑的方法
STL adapter stack and queue
CV目标检测模型小抄(1)
安防摄像头互联网直播方案LiveGBS设计文档
引擎提示Alias HeroDB跟游戏引擎启动异常怎么解决?
固态硬盘复制文件只有几十kb怎么办? 电脑安装硬盘后复制速度慢的解决办法
传奇私服GOM引擎启动M2提示:[X-FKGOM] 已经加载成功卡住的怎么处理?
Design of boiler drum temperature control system (process control course design matlab/simulink)
Realize the national standard gb28181 streaming media service solution
集成底座与业务系统对接过程梳理
尿酸检测与注意事项
锅炉汽包温度控制系统设计(过程控制课程设计matlab/simulink)
小马激活工具出现Cannot open file k:\OEMSF 的解决方法
电脑如何获取administrator权限 电脑管理员权限设置教程
电商项目如何解决线上优惠券超发(排错+解决方案)(荣耀典藏)
Demo19- (to be updated)