DCC 26 AUGUST 2024 || RECURSIVE || ITERATIVE🔥
\n\n# Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n)  For Recursion - : O(N) For Iterative -: O(N)\n\n- Space complexity:\n Add your 
\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> For Recursion - : O(N) For Iterative -: O(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --> For Recursion -: AUxillary Stack Space-: O(N) For Iteration -: O(N)\n\n# Code\n```java []\n# Recrusive Solution\n/*\n// Definition for a Node.\nclass Node {\n    public int val;\n    public List<Node> children;\n\n    public Node() {}\n\n    public Node(int _val) {\n        val = _val;\n    }\n\n    public Node(int _val, List<Node> _children) {\n        val = _val;\n        children = _children;\n    }\n};\n*/\n\nclass Solution {\n    public List<Integer> postorder(Node root) {\n\n        List<Integer> list = new ArrayList<>();\n        helper(root, list);\n        return list;\n    }\n\n    void helper(Node node , List<Integer> list){\n\n        if(node==null) return;\n\n        for(Node temp:node.children){\n            helper(temp,list);\n            // list.add(temp.val);\n        }\n        list.add(node.val);\n    }\n}\n\n# Code\n# Iterative Solution\n\n/*\n// Definition for a Node.\nclass Node {\n    public int val;\n    public List<Node> children;\n\n    public Node() {}\n\n    public Node(int _val) {\n        val = _val;\n    }\n\n    public Node(int _val, List<Node> _children) {\n        val = _val;\n        children = _children;\n    }\n};\n*/\n\nclass Solution {\n    public List<Integer> postorder(Node root) {\n        if(root==null) return new ArrayList<>();\n        Stack<Node> stack = new Stack<>();\n        List<Integer> list = new ArrayList<>();\n        list.add(root.val); \n\n        for(int i = 0;i<root.children.size();i++) stack.push(root.children.get(i));\n\n        while(!stack.isEmpty()){\n\n            Node node = stack.pop();\n            list.add(node.val);\n            for(int i = 0;i<node.children.size();i++) stack.push(node.children.get(i));\n\n        }\n        Collections.reverse(list);\n        return list;\n\n        \n    }\n}\n```
['Tree', 'Java']