首页 > 其他 > 详细

LeetCode 解题报告

时间:2019-02-01 16:19:42      阅读:205      评论:0      收藏:0      [点我收藏+]

题目要求

Given an n-ary tree, return the postorder traversal of its nodes‘ values.

题目分析及思路

题目给出一棵N叉树,要求返回结点值的后序遍历。可以使用递归的方法做。因为是后序遍历,所以最后加入根结点的值。

python代码?

"""

# Definition for a Node.

class Node:

    def __init__(self, val, children):

        self.val = val

        self.children = children

"""

class Solution:

    def postorder(self, root):

        """

        :type root: Node

        :rtype: List[int]

        """

        order = []

        if not root:

            return order

        for child in root.children:

            order.extend(self.postorder(child))

        order.append(root.val)

        return order

        

        

 

LeetCode 解题报告

原文:https://www.cnblogs.com/yao1996/p/10346145.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!