Python Easy Solution | Explanation 🔥🔥
Intuition\nIn postorder traversal of an n-ary tree, we need to visit all children of a node before visiting the node itself. Implementing this iteratively can b
# Intuition\nIn postorder traversal of an n-ary tree, we need to visit all children of a node before visiting the node itself. Implementing this iteratively can be tricky because we need to ensure that all children are processed before the node itself. This can be achieved using a stack to handle the nodes and another data structure to store the nodes in reverse postorder sequence.\n\n# Approach\n1. **Use a stack for traversal:** Initialize a stack with the root node. This stack will help us traverse the tree iteratively.\n2. **Store nodes in reverse postorder:** Use an output list to store nodes as we traverse them. By pushing nodes to this list in a specific order, we can later reverse the list to get the correct postorder traversal.\n3. **Traversal steps:**\n    * Pop a node from the stack.\n    * Push its value to the output list.\n    * Push all its children to the stack.\n4. **Reverse the output list:** Once all nodes are processed, the output list will contain nodes in a reversed postorder sequence. Reversing this list will give us the correct postorder traversal.\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n"""\n# Definition for a Node.\nclass Node:\n    def __init__(self, val=None, children=None):\n        self.val = val\n        self.children = children\n"""\n\nclass Solution:\n    def postorder(self, root: \'Node\') -> List[int]:\n        if not root:\n            return []\n        \n        stack = [root]\n        output = []\n        \n        while stack:\n            node = stack.pop()\n            output.append(node.val)\n            for child in node.children:\n                stack.append(child)\n        \n        return output[::-1]\n```
['Python3']