每日一题力扣589 N叉树的前序遍历

给定一个 N 叉树,返回其节点值的 前序遍历 。

N 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例)。

进阶:

递归法很简单,你可以使用迭代法完成此题吗?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

借了上一题二叉树的前序遍历的思想,也知道了py3里居然还有root.children这种表达

class Solution:
    def preorder(self, root: 'Node') -> List[int]:
        def pre_order(root):
            if not root:
                return
            res.append(root.val)
            for u in root.children:
                pre_order(u)
        res=[]
        pre_order(root)
        return res
原文地址:https://www.cnblogs.com/liuxiangyan/p/14658161.html