Iterative And Recursive Approach | Java | C++ | [Video Solution]
Intuition, approach, and complexity discussed in video solution in detail.\nhttps://youtu.be/lLfg-Q3HDyE\n# Code\n- Recursive Appraoch\njava []\nclass Solution 
# Intuition, approach, and complexity discussed in video solution in detail.\nhttps://youtu.be/lLfg-Q3HDyE\n# Code\n- Recursive Appraoch\n``` java []\nclass Solution {\n    private List<Integer> res = new ArrayList<>();\n    public List<Integer> postorder(Node root) {\n        populateRes(root);\n        return res;\n    }\n    private void populateRes(Node root){\n        if(root == null)return;\n        for(var child : root.children){\n            populateRes(child);\n        }\n        res.add(root.val);\n    }\n}\n```\n```cpp []\nclass Solution {\nprivate:\n    vector<int> res;\n    \n    void populateRes(Node* root) {\n        if(root == nullptr) return;\n        for(auto child : root->children) {\n            populateRes(child);\n        }\n        res.push_back(root->val);\n    }\n    \npublic:\n    vector<int> postorder(Node* root) {\n        populateRes(root);\n        return res;\n    }\n};\n```\n- Iterative Approach\n```java []\nclass Solution {\n    public List<Integer> postorder(Node root) {\n        List<Integer> res = new ArrayList<>();\n        if(root == null)return res;\n        Deque<Node> nodeStack = new ArrayDeque<>();\n        nodeStack.offerLast(root);\n        while(!nodeStack.isEmpty()){\n            Node currNode = nodeStack.pollLast();\n            res.add(currNode.val);\n            for(var child : currNode.children){\n                nodeStack.offerLast(child);\n            }\n        }\n        Collections.reverse(res);\n        return res;\n    }\n}\n```\n``` cpp []\nclass Solution {\npublic:\n    vector<int> postorder(Node* root) {\n        vector<int> res;\n        if(root == nullptr) return res;\n        deque<Node*> nodeStack;\n        nodeStack.push_back(root);\n        while(!nodeStack.empty()){\n            Node* currNode = nodeStack.back();\n            nodeStack.pop_back();\n            res.push_back(currNode->val);\n            for(auto child : currNode->children){\n                nodeStack.push_back(child);\n            }\n        }\n        reverse(res.begin(), res.end());\n        return res;\n    }\n};\n\n```
['Stack', 'Tree', 'Depth-First Search', 'Java']