590. N叉树的后序遍历(简单)

"""
# Definition for a Node.
class Node(object):
    def __init__(self, val, children):
        self.val = val
        self.children = children
"""
class Solution(object):
    def postorder(self, root):
        """
        :type root: Node
        :rtype: List[int]
        """
        if not root:
            return []
        if not root.children:
            return [root.val]
        result=[]
        result.append(root.val)
        for child in root.children[::-1]:    #使得每一层是正序输出
            result=self.postorder(child)+result
        return result

执行用时: 148 ms, 在N-ary Tree Postorder Traversal的Python提交中击败了58.62% 的用户

猜你喜欢

转载自blog.csdn.net/weixin_42234472/article/details/85037479