title: How to write iterative DFS which counts descendants of a node in a tree
body: <p>I have problem with turning this code:</p>
  
  <pre><code>void dfs(int i = 1) {
    static int preorder = 0;
    d[i].first = ++preorder;
    d[i].second = 1;
    for (list&lt;int&gt;::iterator it = tree[i].begin(); it != tree[i].end(); ++it) {
      dfs(*it);
      d[i].second += d[*it].second;
    }
  }
  </code></pre>
  
  <p>into iterative one. As you can see, it finds preorder number of each node and how many descendants it has.
  I have to do it, because of memory limitation (data size is up to 10^6).</p>
  
  <p>Thanks in advance.</p>