给定一个 N 叉树,返回其节点值的前序遍历。
例如,给定一个 3叉树
:
返回其前序遍历: [1,3,5,6,2,4]
。
说明: 递归法很简单,你可以使用迭代法完成此题吗?
**难度**: Easy
**标签**: 树、
# -*- coding: utf-8 -*-
# @Author : LG
"""
执行用时:60 ms, 在所有 Python3 提交中击败了86.96% 的用户
内存消耗:15.2 MB, 在所有 Python3 提交中击败了85.68% 的用户
解题思路:
递归
"""
"""
# 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]:
result =[]
def find(root):
if root:
result.append(root.val)
childrens = root.children
for children in childrens:
find(children)
find(root)
return result