码迷,mamicode.com
首页 > 其他好文 > 详细

589. N-ary Tree Preorder Traversal

时间:2018-11-17 19:08:42      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:set   图片   http   efi   end   eee   分享图片   traversal   rhel   

1. Question 

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:

 

技术分享图片

 

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

 

2. Solution

# Definition for a Node.
class Node(object):
    def __init__(self, val, children):
        self.val = val
        self.children = children


class Solution(object):

    def preOrderHelp(self, root, path):

        if root is None:
            return
        path.append(root.val)
        children = root.children
        for ch in children:
            self.preOrderHelp(ch, path)

    def preorder(self, root):
        """
        :type root: Node
        :rtype: List[int]
        """
        re_li = []
        self.preOrderHelp(root, re_li)
        return re_li

 

589. N-ary Tree Preorder Traversal

标签:set   图片   http   efi   end   eee   分享图片   traversal   rhel   

原文地址:https://www.cnblogs.com/ordili/p/9974913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!