title: Depth First Search without stack
body: <p>I started to code with stack as:</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>
  
  <p>But, without stack I do not know, what I should be doing.</p>
  
  <p>I tried this. Is it correct ?
  Can anyone suggest me.</p>
  
  <pre><code>void Foo(TreeNode root)
  {
     if(root == null) return;
  
      System.out.print(root.Value + "\t");
      root.state = State.Visited;
  
      //for every child
      for(Node n: root.getChild())
      {
          //if childs state is not visited then recurse
          if(n.state == State.Unvisited)
          {
              dfs(n);
          }
      }
  }
  </code></pre>