Easy DFS Solution | Beats 100%
Code\npython []\nclass Solution(object):\n    def postorder(self, root):\n        # If the root is None, return an empty list\n        if not root:\n           
# Code\n```python []\nclass Solution(object):\n    def postorder(self, root):\n        # If the root is None, return an empty list\n        if not root:\n            return []\n\n        res = []\n\n        # Define the DFS function\n        def dfs(root):\n            # Recursively call dfs for each child of the current node\n            for x in root.children:\n                dfs(x)\n            # Append the value of the current node to the result list\n            res.append(root.val)\n\n        # Start DFS from the root\n        dfs(root)\n\n        # Return the result list containing node values in post-order\n        return res\n```\n```C++ []\nclass Solution {\npublic:\n    vector<int> postorder(Node* root) {\n        // If the root is null, return an empty vector\n        if (!root) return {};\n\n        vector<int> res;\n\n        // Define the DFS function\n        function<void(Node*)> dfs = [&](Node* node) {\n            // Recursively call dfs for each child of the current node\n            for (Node* child : node->children) {\n                dfs(child);\n            }\n            // Append the value of the current node to the result vector\n            res.push_back(node->val);\n        };\n\n        // Start DFS from the root\n        dfs(root);\n\n        // Return the result vector containing node values in post-order\n        return res;\n    }\n};\n```\n```Java []\nclass Solution {\n    public List<Integer> postorder(Node root) {\n        // If the root is null, return an empty list\n        if (root == null) return new ArrayList<>();\n\n        List<Integer> res = new ArrayList<>();\n\n        // Start DFS from the root\n        dfs(root, res);\n\n        // Return the result list containing node values in post-order\n        return res;\n    }\n\n    private void dfs(Node root, List<Integer> res) {\n        // Recursively call dfs for each child of the current node\n        for (Node child : root.children) {\n            dfs(child, res);\n        }\n        // Append the value of the current node to the result list\n        res.add(root.val);\n    }\n}\n```\n![unnamed.jpg](https://assets.leetcode.com/users/images/c601cf11-3709-4b4e-b489-9134d2c166f4_1724632241.9653273.jpeg)\n
['Stack', 'Tree', 'Depth-First Search', 'Python', 'C++', 'Java', 'Python3']