title: How to convert the recursive TreeDfs algo into iterative algo?
body: <p>Can we convert following recursive tree DFS algo into iterative way? it seems much harder than normal binary tree recursive-&gt;iterative conversion. Does anyone have some idea on it?</p>
  <pre><code>struct Edge {
     int vertex;
     int next;
  };
  
  int head[MAXN];
  Edge edge[MAXN*2];  //edge[0] is used as NIL
  int size[MAXN];
  int heavy[MAXN];
  
  void TreeDfs(int u, int f)
  {
     int idx;
     int max_child_size = 0;
     size[u] = 1;
     heavy[u] = -1; //not existing
     idx = head[u];
     while (idx != 0) {
       int v = edge[idx].vertex;
       if (v != f) {
          TreeDfs(v, u);
          size[u] += size[v];
          if (size[v] &gt; max_child_size) {
            max_child_size = size[v];
            heavy[u] = v;
          }
       }
       idx = edge[idx].next;
     }
  }
  </code></pre>