title: TreeNode stack Depth First Search
body: <p>I saw this topic: Depth First Search without stack [closed]. I have a question related to this function. I want to use stack.
  As I am C# beginner and a wondering if someone can explain me in detail 1) what the following program goal and 2) what the expected output. </p>
  
  <pre><code>void Foo(TreeNode root)
  {
      Stack nodes = new Stack();
      nodes.Push(root);
  
      while (nodes.Count &gt; 0)
      {
          TreeNode node = (TreeNode) nodes.Pop();
          Console.WriteLine(node.Text);
          for (int i = node.Nodes.Count - 1; i &gt;= 0; i--)
              nodes.Push(node.Nodes[i]);
      }
  }
  </code></pre>