给定一个 N 叉树,返回其节点值的前序遍历。
例如,给定一个 3叉树 :
返回其前序遍历: [1,3,5,6,2,4]。
说明: 递归法很简单,你可以使用迭代法完成此题吗?
迭代法:
"""
# 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==None:
return []
list = []
stack = []
list.append(root)
while(list):
temp = list.pop()
stack.append(temp.val)
if temp.children: #如果该元素有子节点children 则反转加入到 stack 里(因为是前序遍历)
for item in temp.children[::-1]:
list.append(item)
return stack