预先遍历深度优先算法吗?我在下面的搜索中使用它。我已经包含下面的代码。预先遍历深度优先方法吗?
public bool DFS1(int value, BSTNode root)
{ // Pre-order search
if (root == null)
return false;
if (root.data == value)
{
Console.WriteLine("found");
return true;
}
DFS1(value, root.left); //vist the left node
return DFS1(value, root.right); // vist the right node.
}
你忽略了左递归的结果。如果左侧成功,则不需要正确的搜索。 – molbdnilo
@ A.Sarid基本问题是重复的。然而OP在双递归中存在问题以解决(恕我直言)保证打开这个问题。 – Prune