• 589. N-ary Tree Preorder Traversal


    Given an n-ary tree, return the preorder traversal of its nodes' values.

    For example, given a 3-ary tree:

    avatag

    Return its preorder traversal as: [1,3,5,6,2,4].

    # Definition for a Node.
    class Node(object):
        def __init__(self, val, children):
            self.val = val
            self.children = children
    
    
    
    class Solution(object):
        def preorder(self, root):
            """
            :type root: Node
            :rtype: List[int]
            """
            res = []
            if not root:
                return []
            res.append(root.val)
            for child in root.children:
                res.extend(self.preorder(child))
            return res
    
  • 相关阅读:
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    C语言基础知识【作用域规则】
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/9797529.html
Copyright © 2020-2023  润新知