
简述
前序遍历 N 叉树
n-ary-tree-preorder-traversal 英文 中文
收获
list 官方文档 菜鸟教程
代码
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 (object): def preorder(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[::-1]) return output
|
近期评论