☑️✅Beats-100% Depth First Search || Recursion || Beginner Friendly
\u2705 IF YOU LIKE THE APPROACH DO UPVOTE \u261D\uFE0F\u2B06\uFE0F\uD83D\uDD1D\uD83D\uDCC8\n\n\n### Approach\n\nIn this problem, the goal is to perform a postor
# \u2705 IF YOU LIKE THE APPROACH DO UPVOTE \u261D\uFE0F\u2B06\uFE0F\uD83D\uDD1D\uD83D\uDCC8\n\n![goats (Small).jpeg](https://assets.leetcode.com/users/images/932d7473-86a2-49d9-bb65-1412c4497d31_1724650616.8388345.jpeg)\n### Approach\n\nIn this problem, the goal is to perform a **postorder traversal** of an N-ary tree. In postorder traversal, you visit all the children of a node before visiting the node itself.\n\n**Steps:**\n1. **Recursive Traversal**: The algorithm uses recursion to explore each node\'s children first and then the node itself.\n2. **Base Case**: If the root is `null`, the function returns an empty list, as there is nothing to traverse.\n3. **Recursive Step**: For each node, the algorithm recursively calls the function on all of its children. After visiting all the children, it appends the node\'s value to the result list.\n\n### Code\n``` java []\nclass Solution {\n    // List to store the postorder traversal result\n    ArrayList<Integer> result = new ArrayList<>();\n\n    public List<Integer> postorder(Node root) {\n        if (root == null) return result;\n\n        // Recursively call postorder on each child\n        for (Node child : root.children) {\n            postorder(child);\n        }\n\n        // After all children are processed, add the current node\'s value\n        result.add(root.val);\n\n        return result;\n    }\n}\n\n```\n\n```python []\nclass Solution:\n    def __init__(self):\n        self.result = []\n\n    def postorder(self, root: \'Node\') -> List[int]:\n        if not root:\n            return self.result\n        \n        for child in root.children:\n            self.postorder(child)\n        self.result.append(root.val)\n        \n        return self.result\n```\n\n```cpp []\nclass Solution {\npublic:\n    vector<int> result;\n    \n    vector<int> postorder(Node* root) {\n        if (!root) return result;\n        \n        for (Node* child : root->children) {\n            postorder(child);\n        }\n        result.push_back(root->val);\n        \n        return result;\n    }\n};\n```\n\n### Key Points\n- **Recursion** is used to simplify the traversal, ensuring that all children of a node are processed before the node itself.\n- The **base case** handles when the root is `null`, ensuring the function doesn\u2019t attempt to process an empty tree.\n- The **result list** (or vector) accumulates the values in postorder, which means children are visited first, then the node.\n\nThis approach is straightforward and leverages recursion to naturally traverse the tree in postorder, making it easy to understand and implement in both Python and C++.\n\n\u2705 **IF YOU LIKE THE APPROACH DO UPVOTE** \u261D\uFE0F\u2B06\uFE0F\uD83D\uDD1D\uD83D\uDCC8\nLets Gooo...\n\n![b0fe2c38-f59f-4a6a-8092-ecf02e3e99b0_1703608389.4843047.gif](https://assets.leetcode.com/users/images/6f5f3b10-1612-4555-978d-7bbfccaf3152_1703694297.0998082.gif)
['Tree', 'Depth-First Search', 'C++', 'Java', 'Python3']