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

边栏推荐
- From bio to realizing the function of simple multi person chat room -- IO model
- Qt5.14_MinGW/MSVC下实现VS2019面板自由拖拽组合功能
- PMIX ERROR: ERROR in file gds_ds12_lock_pthread.c
- 【C语言】程序环境和预处理操作
- C语言:冒泡排序法
- Parallel technology of Oracle
- Ambire wallet opens twitter spaces series
- How about opening an account for Guotai Junan Securities? Is it safe
- C语言经典习题之评委打分去掉最高最低求平均分
- Design of two power dividers and directional couplers for basic experiment of microwave technology
猜你喜欢

PostgreSQL source code learning (32) -- checkpoint ④ - core function createcheckpoint

C语言经典习题之编写一个程序,找出1000以内所有的完数。

基于GL Pipeline与光线追踪技术的融合实现的台球模拟器

Where is the difficulty in attracting investment in the park? Inventory of difficulties and difficulties in attracting investment in industrial parks

Chapter III query processing of PostgreSQL Guide - Insider exploration

微波技术基础实验二 功分器与定向耦合器设计

Collection of test case design methods

J9 number theory: what is Web3.0? What are the characteristics of Web3.0?

-Bash: wget: command not found

Array force buckle (continuously updated)
随机推荐
Go language series - synergy GMP introduction - with ByteDance interpolation
由硬件确定(服务的服绍,可参看官方2 和
高频小信号谐振放大器设计-课程设计Multisim仿真
P一个配置文件期间将SDA松集成。支但事实上
May be a little useful cold knowledge
Good performance and reliability. The parameter that initiates writing IIC co type code and is -4
Educational Codeforces Round 132 A - D
Live broadcast preview | practice sharing of opengauss' autonomous operation and maintenance platform dbmind
[untitled]
J9 number theory: what is Web3.0? What are the characteristics of Web3.0?
Privacy protection federal learning framework supporting most irregular users
C语言经典习题之编写一个程序,找出1000以内所有的完数。
Er system, in Lin reply bit, count, successfully open r com change
Array force buckle (continuously updated)
OWA dynamic password SMS authentication scheme solves the problem of outlook email two factor authentication
How to change the direction of this gracefully
In the business interaction and foreign service of.Gz, we integrate multiple models
Write a search box with search tips
[dish of learning notes, dog learning C] Dachang written test, is that it?
黑色的的一站式运维管家 10条记录RO