Simple Java || C++ Code ☠️
\n\n# Code\n\n\njava []\n/*\n// Definition for a Node.\nclass Node {\n    public int val;\n    public List<Node> children;\n\n    public Node() {}\n\n    public
\n\n# Code\n\n\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    public List<Integer> postorder(Node root) {\n        ArrayList<Integer> list = new ArrayList<>();\n        if(root==null){\n            return list; \n        }\n        help(list,root);\n        list.add(root.val);\n        return list;\n    }\n    public void help(ArrayList<Integer> list,Node root){\n        List<Node> li = new ArrayList<>();\n        li = root.children;\n        for(int i=0;i<li.size();i++){\n            if(li.get(i)==null){\n                continue;\n            }\n            help(list,li.get(i));\n            list.add(li.get(i).val);\n        }\n    }\n}\n```\n```C++ []\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        vector<int> list;\n        if(root==NULL){\n            return list; \n        }\n        help(list,root);\n        list.push_back(root->val);\n        return list;\n    }\n    void help(vector<int>& list,Node* root){\n        vector<Node*> li;\n        li = root->children;\n        for(int i=0;i<li.size();i++){\n            if(li[i]==NULL){\n                continue;\n            }\n            help(list,li[i]);\n            list.push_back(li[i]->val);\n        }\n    }\n};\n```\n\n\n\n\n\n### Explanation\n\nThe provided code consists of a class `Solution` with two main methods:\n1. `postorder(Node root)` - This is the main method that initiates the postorder traversal.\n2. `help(ArrayList<Integer> list, Node root)` - A helper method to recursively perform postorder traversal.\n\n#### Class and Method Breakdown\n\n\n\n1. **postorder Method**\n   ```java\n   public List<Integer> postorder(Node root) {\n       ArrayList<Integer> list = new ArrayList<>();\n       if (root == null) {\n           return list;\n       }\n       help(list, root);\n       list.add(root.val);\n       return list;\n   }\n   ```\n   - **Parameters:**\n     - `Node root`: The root node of the n-ary tree.\n   - **Logic:**\n     - Initializes an `ArrayList<Integer> list` to store the postorder traversal result.\n     - If the `root` is `null`, it returns the empty `list` immediately.\n     - Calls the helper method `help(list, root)` to recursively traverse the tree in postorder.\n     - After the recursive calls, it adds the `root` node\'s value to the `list` (ensuring postorder traversal).\n     - Returns the `list` containing the postorder traversal of the tree.\n\n3. **help Method**\n   ```java\n   public void help(ArrayList<Integer> list, Node root) {\n       List<Node> li = root.children;\n       for (int i = 0; i < li.size(); i++) {\n           if (li.get(i) == null) {\n               continue;\n           }\n           help(list, li.get(i));\n           list.add(li.get(i).val);\n       }\n   }\n   ```\n   - **Parameters:**\n     - `ArrayList<Integer> list`: The list that collects the node values in postorder.\n     - `Node root`: The current node being processed.\n   - **Logic:**\n     - Retrieves the children of the current node (`root`) using `root.children`.\n     - Iterates through each child node:\n       - If the child is `null`, it skips to the next iteration.\n       - Recursively calls `help(list, li.get(i))` to process the subtree rooted at the child node `li.get(i)`.\n       - After the recursive call returns, it adds the value of the current child node (`li.get(i).val`) to the `list`.\n   - This method ensures that all children are processed in postorder before adding the current node\'s value to the list.\n\n### How the Code Works (Step-by-Step Walkthrough)\n\nLet\'s go through an example to understand how this code performs a postorder traversal:\n\n**Example Tree Structure**:\n```\n       1\n     / | \\\n    3  2  4\n   / \\\n  5   6\n```\n\n**Postorder Traversal**: `[5, 6, 3, 2, 4, 1]`\n\n1. **Call `postorder(root)` with `root` as node `1`**:\n   - Initialize `list = []`.\n   - Call `help(list, root)` with `root` as node `1`.\n\n2. **First call to `help(list, 1)`**:\n   - `li = [3, 2, 4]` (children of node `1`).\n   - Start iterating through `li`.\n\n3. **Processing child node `3` (first child of node `1`)**:\n   - Call `help(list, 3)`.\n   - `li = [5, 6]` (children of node `3`).\n   - Start iterating through `li`.\n\n4. **Processing child node `5` (first child of node `3`)**:\n   - Call `help(list, 5)`.\n   - `li = []` (no children).\n   - Return from `help(list, 5)`.\n   - Add `5` to `list`, making `list = [5]`.\n\n5. **Processing child node `6` (second child of node `3`)**:\n   - Call `help(list, 6)`.\n   - `li = []` (no children).\n   - Return from `help(list, 6)`.\n   - Add `6` to `list`, making `list = [5, 6]`.\n\n6. **Returning to `help(list, 3)`**:\n   - All children processed; add `3` to `list`.\n   - `list = [5, 6, 3]`.\n\n7. **Processing child node `2` (second child of node `1`)**:\n   - Call `help(list, 2)`.\n   - `li = []` (no children).\n   - Return from `help(list, 2)`.\n   - Add `2` to `list`, making `list = [5, 6, 3, 2]`.\n\n8. **Processing child node `4` (third child of node `1`)**:\n   - Call `help(list, 4)`.\n   - `li = []` (no children).\n   - Return from `help(list, 4)`.\n   - Add `4` to `list`, making `list = [5, 6, 3, 2, 4]`.\n\n9. **Returning to `postorder(root)`**:\n   - All children processed; add `1` (the root) to `list`.\n   - `list = [5, 6, 3, 2, 4, 1]`.\n\n10. **Return final list**: `[5, 6, 3, 2, 4, 1]`.\n\n### Complexity Analysis\n\n- **Time Complexity**: \\(O(n)\\), where \\(n\\) is the number of nodes in the tree. Each node is visited exactly once.\n- **Space Complexity**: \\(O(n)\\) in the worst case due to the recursive call stack and the list holding all node values.\n\n\n\n\n\n\n
['Tree', 'Depth-First Search', 'C++', 'Java']