C++ || Iterative Solution || Stack
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is to perform a postorder traversal on an N-ary tree, where each node can h
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is to perform a postorder traversal on an N-ary tree, where each node can have multiple children. The approach is similar to the postorder traversal of a binary tree. However, instead of handling just left and right children, we need to consider all the children of each node.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe iterative approach uses a stack to traverse the tree. We push the root node onto the stack, then iteratively pop nodes, adding their values to the result. For each node, we push all its children onto the stack. Since the postorder traversal visits nodes in the order \u201Cchildren first, then the root,\u201D we reverse the result at the end to get the correct order.\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```cpp []\n/*\n// Definition for a Node.\nclass Node {\npublic:\n    int val;\n    vector<Node*> children;\n\n    Node() {}\n\n    Node(int _val) {\n        val = _val;\n    }\n\n    Node(int _val, vector<Node*> _children) {\n        val = _val;\n        children = _children;\n    }\n};\n*/\n\nclass Solution {\npublic:\n    vector<int> postorder(Node* root) {\n        if(root==nullptr) \n            return {};\n\n        vector<int> ans;\n        stack<Node*> st;\n        st.push(root);\n\n        while(!st.empty()){\n            auto node = st.top();\n            ans.push_back(node->val);\n            st.pop();\n            \n            for(auto it: node->children){\n                if(it)\n                    st.push(it);\n            }\n        }\n\n        reverse(ans.begin(), ans.end());\n\n        return ans;\n    }\n};\n```
['Stack', 'Tree', 'C++']