当前位置:网站首页>Little black gnawing leetcode:589. Preorder traversal of n-ary tree
Little black gnawing leetcode:589. Preorder traversal of n-ary tree
2022-07-24 04:32:00 【Xiaohei invincible】
Recursive method
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
class Solution:
def preorder(self, root: 'Node') -> List[int]:
# Define the storage array
arr = []
def dfs(node):
if node:
arr.append(node.val)
# Recursively traverse child nodes in turn
for child in node.children:
dfs(child)
dfs(root)
return arr

Non recursive method
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

边栏推荐
- CONDA common commands
- NFT除了买卖还能质押?
- Traversal of binary tree
- .gz的业务交互和对外服篇中我们通合多个模型
- Shell syntax (1)
- Shell语法(一)
- May be a little useful cold knowledge
- Good performance and reliability. The parameter that initiates writing IIC co type code and is -4
- Excel cell formula - realize Ackerman function calculation
- Billiard simulator based on the integration of GL pipeline and ray tracing technology
猜你喜欢

Smart contract: release an erc20 token

归并排序(Merge sort)

Alibaba Taobao Department interview question: how does redis realize inventory deduction and prevent oversold?

C语言经典习题之猴子吃桃问题

"Paper reproduction" bidaf code implementation process (3) model establishment

ARP Spoofing protection of network security

A hospital call system based on C language

Clickpaas, a low code service provider, has completed a strategic merger with BiP technology to jointly build an industrial digital base
![[translation] announce krius -- accelerate your monitoring and adoption of kubernetes](/img/6c/be19a910e60da701a054c4bf689000.jpg)
[translation] announce krius -- accelerate your monitoring and adoption of kubernetes

基于C语言设计的一个医院叫号系统
随机推荐
你有多久没有换新手机了?
Smart contract: release an erc20 token
Forward proxy, reverse proxy and XFF
Privacy protection federal learning framework supporting most irregular users
Nautilus 3.19.2 adds momentum to Gnome
CONDA common commands
Oracle的并行技术
Collection of test case design methods
Ambire wallet opens twitter spaces series
C语言:选择排序法
To -.---
归并排序(Merge sort)
P一个配置文件期间将SDA松集成。支但事实上
Design of high frequency small signal resonant amplifier course design Multisim Simulation
[hope to answer] the data cannot be synchronized correctly
C语言:随机数的生成
C语言:冒泡排序法
基于C语言设计的一个医院叫号系统
Baidu search cracking down on pirated websites: why Internet content infringement continues despite repeated prohibitions
C语言经典习题之猴子吃桃问题