题目链接:589. N 叉树的前序遍历
迭代解法。
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution:
def preorder(self, root: 'Node') -> List[int]:
if root is None:
return []
stack, ret = [root], []
while stack:
node = stack.pop()
ret.append(node.val)
for n in (node.children or [])[::-1]:
stack.append(n)
return ret