当前位置:网站首页>Journey of little black leetcode: 590. Post order traversal of n-ary tree
Journey of little black leetcode: 590. Post order traversal of n-ary tree
2022-07-24 04:32:00 【Xiaohei invincible】
1. Non recursive method
""" # 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. Recursive method
""" # 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. Root first traversal reversal method
""" # 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)

边栏推荐
- 【ARC127F】±AB
- Codeforces Round #807 (Div. 2) A - D
- Live broadcast preview | practice sharing of opengauss' autonomous operation and maintenance platform dbmind
- 工程师能力模型与技能要求
- The judges of C language classic exercises score the highest and lowest to get an average score
- The second anniversary of open source, opengauss Developer Day 2022 full highlights review!
- 到3mm;提供安全稳定的产品作的执行据发出方iid
- Design and implementation of data analysis platform for intelligent commerce
- 微波技术基础实验二 功分器与定向耦合器设计
- [untitled]
猜你喜欢

Design of high frequency small signal resonant amplifier course design Multisim Simulation

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

链接预测中训练集、验证集以及测试集的划分(以PyG的RandomLinkSplit为例)

C language classic exercises to write a program to find all the perfects within 1000.

A hospital call system based on C language

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

-Bash: wget: command not found

Avoid mistakes, common appium related problems and Solutions

OWA dynamic password SMS authentication scheme solves the problem of outlook email two factor authentication

Analyze the real-time development method of Bank of London
随机推荐
An online accident, I suddenly realized the essence of asynchrony
Shell语法(二)
J9 number theory: what is Web3.0? What are the characteristics of Web3.0?
Alibaba Taobao Department interview question: how does redis realize inventory deduction and prevent oversold?
-Bash: wget: command not found
Baidu search cracking down on pirated websites: why Internet content infringement continues despite repeated prohibitions
Ambire wallet opens twitter spaces series
致-.-- -..- -
[2023 core technology approval test questions in advance] ~ questions and reference answers
PostgreSQL guide -- inside exploration Chapter 1 database clusters, databases and data tables
Graduation thesis on enterprise production line improvement [Flexsim simulation example]
Privacy protection federal learning framework supporting most irregular users
C语言:随机数的生成
Oracle的并行技术
PostgreSQL source code learning (32) -- checkpoint ④ - core function createcheckpoint
Array force buckle (continuously updated)
Label smoothing
Up sampling method (deconvolution, interpolation, anti pooling)
C语言经典习题之猴子吃桃问题
dispatch_ Once's Secret