1. 程式人生 > >【leetcode】590. N-ary Tree Postorder Traversal

【leetcode】590. N-ary Tree Postorder Traversal

inf clas traversal val obj 解題思路 tor sel spa

題目如下:

技術分享圖片

解題思路:湊數題+2,做完先序做後序。湊數博+2。

代碼如下:

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

【leetcode】590. N-ary Tree Postorder Traversal