1. 程式人生 > >【leetcode】589. N-ary Tree Preorder Traversal

【leetcode】589. N-ary Tree Preorder Traversal

pan 題目 info children class for pre 技術分享 分享圖片

題目如下:

技術分享圖片

解題思路:湊數題+1,話說我這個也是湊數博?

代碼如下:

class Solution(object):
    def preorder(self, root):
        """
        :type root: Node
        :rtype: List[int]
        """
        if root == None:
            return []
        res = []
        stack = [root]
        while len(stack) > 0:
            node 
= stack.pop(0) res.append(node.val) for i in node.children[::-1]: stack.insert(0,i) return res

【leetcode】589. N-ary Tree Preorder Traversal