【发布时间】:2017-01-22 15:20:14
【问题描述】:
前序遍历是深度优先算法吗?我在下面的搜索中使用它。我在下面包含了代码。
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.
}
【问题讨论】:
-
您忽略了左递归的结果。如果左搜索成功,则不需要右搜索。
-
@A.Sarid 基本问题是重复的。然而,OP 在解决该问题的双重递归方面存在问题(恕我直言)保证这个问题保持开放。
标签: algorithm recursion binary-search-tree depth-first-search