0
我開始與堆棧的代碼:深度優先搜索無堆
void Foo(TreeNode root)
{
Stack nodes = new Stack();
nodes.Push(root);
while (nodes.Count > 0)
{
TreeNode node = (TreeNode) nodes.Pop();
Console.WriteLine(node.Text);
for (int i = node.Nodes.Count - 1; i >= 0; i--)
nodes.Push(node.Nodes[i]);
}
}
但是,沒有棧我不知道,我應該做的。
我試過了。這是對的嗎 ? 任何人都可以建議我。
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);
}
}
}
你有什麼具體問題?請參閱http://stackoverflow.com/help/how-to-ask和http://stackoverflow.com/help/mcve尋求幫助。 – jordanhill123 2014-09-06 01:41:01
而且,請提醒一位老傢伙在這種情況下「DFS」的含義是什麼? – 2014-09-06 01:41:23
是的,你剛剛發佈了一些Java代碼... – 2014-09-06 01:42:46