N-ary tree postorder Traversal (Easy Explanation - Beats 100%) - 🌟🔜
Intuition\n Describe your first thoughts on how to solve this problem. \nIn the postorder traversal traversal of n-ary tree,first the children of the root are v
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn the postorder traversal traversal of n-ary tree,first the children of the root are visited from left to right and then the root will be visited.\n\nThe children of the n-ary tree is given as List.Since it may contains n-children.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Using recursion to do the postorder traversal of the n-ary tree.\n- The post() has return type as void is called with the parameter root which will be suitable for postorder Traversal.\n- Then the children of the each root is visited so that the children in the tree are visited left to right by find size of the children List and iterate till siz with respect to root,post is called with the node in the children list.\n- Then add the value of the root to the List that contains the postorder Traversal of List.\n- The List contains postorder Traversal of the n-ary tree is returned.\n\n# Complexity\n- *Time complexity*:**O(n)**.\n   n -> number of nodes in the tree.\n- The post function is called exactly once for each node in the tree. If there are n nodes in the tree, the post function will be called n times.\n- For each node, the post function processes all its children in a loop. The sum of the number of children processed over all nodes is n - 1 (since a tree with n nodes has n - 1 edges).\n- Thus, each node and its children are processed in constant time relative to the number of children, making the time complexity linear.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- *Space complexity*:**O(n)**.\n    n -> number of nodes in the tree.\n- **O(h)** for the call stack, where h is the height of the tree.\n- **O(n)** for the node list.\n- **Overall Space Complexity**: O(n) Since List dominates the call stack for large `n`\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\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      private List<Integer> node=new ArrayList<>();\n    private void post(Node root)\n    {\n        if(root!=null)\n        {\n            int siz=root.children.size();\n            //System.out.println(siz);\n            for(int i=0;i<siz;i++)\n            {\n                post(root.children.get(i));\n            }\n            node.add(root.val);\n\n        }\n    }\n    public List<Integer> postorder(Node root) {\n        post(root);\n        return node;\n    }\n}\n```
['Tree', 'Java']