✅☑️ Best C++ solution || DFS || Stack || Recursive and Iterative || One Stop Solution.
Intuition\n Describe your first thoughts on how to solve this problem. \nWe can solve this question using Two method with same complexity.\n\n1. Recursive Appro
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can solve this question using Two method with same complexity.\n\n1. Recursive Approach using DFS + Binary Tree.\n2. Iterative Approach using Stack + Binary Tree.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can easily understand the All the approaches by seeing the code which is easy to understand with comments\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nTime complexity is given in code comment\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nSpace complexity is given in code comment\n\n# Code\n```\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\n\n/*\n\n    Time Complexity : O(N) Where N is the number of Nodes in the tree.\n    Space Complexity : O(N) recursion stack space.\n\n    Solved using DFS + Tree. (Recursive Approach)\n\n*/\n\nclass Solution {\nprivate: \n    void postorder(Node* root, vector<int>& postOrderTraversal){\n        if(root == NULL){\n            return;\n        }\n        for(int i=0; i<root->children.size(); i++){\n            postorder(root->children[i], postOrderTraversal);\n        }\n        postOrderTraversal.push_back(root->val);\n    }\npublic:\n    vector<int> postorder(Node* root) {\n        vector<int> postOrderTraversal;\n        postorder(root, postOrderTraversal);\n        return postOrderTraversal;\n    }\n};\n\n\n\n\n\n\n/*\n\n    Time Complexity : O(N) Where N is the number of Nodes in the tree.\n    Space Complexity : O(N) stack space.\n\n    Solved using Stack + Binary Tree. (Iterative Approach)\n\n*/\n\nclass Solution {\npublic:\n    vector<int> postorder(Node* root) {\n        vector<int> postOrderTraversal;\n        if(root == NULL){\n            return postOrderTraversal;\n        }\n        stack<Node*> store;\n        store.push(root);\n        while(!store.empty()){\n            root = store.top();\n            store.pop();\n            postOrderTraversal.push_back(root->val);\n            for(int i=0; i<root->children.size(); i++){\n                store.push(root->children[i]);\n            }\n        }\n        reverse(postOrderTraversal.begin(), postOrderTraversal.end());\n        return postOrderTraversal;\n    }\n};\n\n```\n\n***IF YOU LIKE THE SOLUTION THEN PLEASE UPVOTE MY SOLUTION BECAUSE IT GIVES ME MOTIVATION TO REGULARLY POST THE SOLUTION.***\n\n![WhatsApp Image 2023-02-10 at 19.01.02.jpeg](https://assets.leetcode.com/users/images/0a95fea4-64f4-4502-82aa-41db6d77c05c_1676054939.8270252.jpeg)
['Tree', 'Depth-First Search', 'Breadth-First Search', 'C++']