1. 程式人生 > >leetcode - 589 - N叉樹的前序遍歷

leetcode - 589 - N叉樹的前序遍歷

"""
# 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]
        """

        if root is None:
            return []
        if root.children is None:
            return [root.val]
        L = [root.val]
        for child in root.children:
            L += self.preorder(child)
        return L

#有時間補一下迭代法解答