Clean C# solution with iterative DFS
I got stack overflow on non-iterative DFS.\n# Code\n\npublic class Solution {\n  public long[] PlacedCoins(int[][] edges, int[] cost) {\n    var graph = BuildGr
I got stack overflow on non-iterative DFS.\n# Code\n```\npublic class Solution {\n  public long[] PlacedCoins(int[][] edges, int[] cost) {\n    var graph = BuildGraph(edges);\n    var placedCoins = new CoinsData[cost.Length];\n\n    Dfs(graph, cost, placedCoins);\n\n    var result = new long[cost.Length];\n    for (int i = 0; i < cost.Length; ++i) {\n      result[i] = placedCoins[i].PlacedCoins;\n    }\n\n    return result;  \n  }\n\n  private List<int>[] BuildGraph(int[][] edges) {\n    var n = edges.Length + 1;\n    var graph = new List<int>[n];\n    for (int i = 0; i < n; ++i) {\n      graph[i] = new();\n    }\n\n    foreach (var edge in edges) { \n      graph[edge[0]].Add(edge[1]);\n      graph[edge[1]].Add(edge[0]);\n    }\n    \n    return graph;\n  }\n\n  private void Dfs(List<int>[] graph, \n      int[] cost, \n      CoinsData[] placedCoins) {\n    var stack = new Stack<(int node, int parent)>();\n    stack.Push((0, -1));\n\n    while (stack.Count > 0) {\n      var (node, parent) = stack.Peek();\n      var children = graph[node].Where(c => c != parent);\n      foreach (var child in children) {\n        if (placedCoins[child] == null) {\n          stack.Push((child, node));\n        }\n      }\n      \n      if (stack.Peek().node != node) {\n        continue;\n      }\n      stack.Pop();\n\n      var positive = new List<int>();\n      var negative = new List<int>();\n      if (cost[node] > 0) { \n        positive.Add(cost[node]);\n      } else {\n        negative.Add(cost[node]);\n      }\n\n      foreach (var child in children) {\n        positive.AddRange(placedCoins[child].MaxPositive);\n        negative.AddRange(placedCoins[child].MaxNegative);\n      }\n\n      positive.Sort((a, b) => b - a);\n      negative.Sort((a, b) => a - b);\n\n      long coinsForNode = 0;\n      if (positive.Count > 2) {\n        coinsForNode = (long)positive[0] * positive[1] * positive[2];\n      }\n      \n      if (negative.Count > 1 && positive.Count > 0) {\n        coinsForNode = Math.Max(coinsForNode,\n            (long)positive[0] * negative[0] * negative[1]);\n      }\n\n      if (positive.Count + negative.Count < 3) {\n        coinsForNode = 1;   \n      } \n      \n      placedCoins[node] = new (coinsForNode, \n        positive.Take(3).ToList(), \n        negative.Take(2).ToList()); \n    }\n  }\n\n  private class CoinsData {\n    public CoinsData() {}\n\n    public CoinsData(long placedCoins, \n        List<int> maxPositive, \n        List<int> maxNegative) {\n      PlacedCoins = placedCoins;\n      MaxPositive = maxPositive;\n      MaxNegative = maxNegative;\n    }\n \n    public long PlacedCoins { get;}\n    public List<int> MaxPositive { get; }\n    public List<int> MaxNegative { get; }\n  }\n}\n```
['C#']