C# Solution for N-ary Tree Postorder Traversal Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind this approach is to use a stack to simulate the traversal of the n
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this approach is to use a stack to simulate the traversal of the n-ary tree iteratively. Instead of directly performing a postorder traversal (which would require visiting all children before the parent), we perform a modified preorder traversal where we process the node first and then its children in a specific order. By inserting the node\u2019s value at the beginning of the result list, we effectively build the postorder sequence in reverse.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tModified Preorder Traversal:\n\t\u2022\tPerform a preorder traversal but push the node\u2019s children onto the stack in reverse order (right-to-left). This allows the leftmost child to be processed last, which aligns with the postorder traversal requirement.\n\t\u2022\tInstead of appending the node\u2019s value to the result list, insert it at the beginning. This results in the correct postorder sequence without needing to reverse the list at the end.\n2.\tProcessing:\n\t\u2022\tStart by pushing the root node onto the stack.\n\t\u2022\tWhile the stack is not empty, pop a node from the stack and insert its value at the start of the result list.\n\t\u2022\tPush the children of the node onto the stack from left to right (so they are processed in reverse order).\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n): The algorithm visits each node exactly once, where n is the total number of nodes in the tree. The insertion operation at the beginning of the list is O(1), making the traversal linear in time complexity.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\u2022\tO(n):\n\t\u2022\tStack Space: In the worst case, the stack may need to store all nodes in the tree simultaneously, especially in the case of a tree with a deep hierarchy.\n\t\u2022\tResult List: The result list will store the values of all nodes, requiring O(n) space.\n\n# Code\n```csharp []\n/*\n// Definition for a Node.\npublic class Node {\n    public int val;\n    public IList<Node> children;\n\n    public Node() {}\n\n    public Node(int _val) {\n        val = _val;\n    }\n\n    public Node(int _val, IList<Node> _children) {\n        val = _val;\n        children = _children;\n    }\n}\n*/\n\npublic class Solution {\n    public IList<int> Postorder(Node root) {\n        IList<int> result = new List<int>();\n        if (root == null) {\n            return result;\n        }\n\n        Stack<Node> stack = new Stack<Node>();\n        stack.Push(root);\n\n        while (stack.Count > 0) {\n            Node node = stack.Pop();\n            result.Insert(0, node.val);\n\n            foreach (var child in node.children) {\n                stack.Push(child);\n            }\n        }\n\n        return result;\n    }\n}\n```
['C#']