n-ary-tree-postorder-traversal

简述

后序遍历 N 叉树
n-ary-tree-postorder-traversal 英文 中文

收获

二叉树

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def postorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
if root is None:
return []

stack, output = [root, ], []
while stack:
root = stack.pop()
output.append(root.val)
stack.extend(root.children)

return output[::-1]
文章目录
  1. 简述
  2. 收获
  3. 代码
|