Problem statement

https://leetcode.com/problems/n-ary-tree-postorder-traversal/

Solution

Very similar to Problem 0589, but here we need to traverse cur.children and then reverse out in the end.

Complexity

Time and space complexity is O(n).

Code

class Solution:
    def postorder(self, root):
        if not root: return []
        stack, out = [root], []
        while stack:
            cur = stack.pop()
            out.append(cur.val)
            for child in cur.children:
                stack += [child]
        return out[::-1]