C# BST中迭代解与递归解的比较
这是我的二叉搜索树函数的一个迭代解决方案,用于查找C#中树的最小值和最大值。我想将其更改为递归,但该代码似乎不在这里C# BST中迭代解与递归解的比较,c#,data-structures,C#,Data Structures,这是我的二叉搜索树函数的一个迭代解决方案,用于查找C#中树的最小值和最大值。我想将其更改为递归,但该代码似乎不在这里 public double FindMin() { Node current = root; while (!(current.left == null)) current = current.left; return current.Data; } public double FindMax() { Node current =
public double FindMin()
{
Node current = root;
while (!(current.left == null))
current = current.left;
return current.Data;
}
public double FindMax()
{
Node current = root;
while (!(current.right == null))
current = current.right;
return current.Data;
}
那么,您能告诉我这段代码有什么问题吗?您可能需要检查类似的问题;那里的解决方案应该具有指导意义
此外,对于递归解决方案,它应该发出一个危险信号,表示它从不考虑正确的子级
public double RecurfindMax(Node current)
{
//current = root;
if (current.left == null)
{
return -1;
}
else
//if (current.left != null)
{
return RecurfindMax(current = current.left);
//return current;
}
这里是递归FIND MIN的真正实现。谢谢,我认为递归的问题在于flag。谢谢
private Node FindMinRecHelper(Node current)
{
if (current.LeftNode == null)
{
return current;
}
else
{
return FindMinRecHelper(current.LeftNode);
}
}
public void FindMinRec()
{
Node current = FindMinRecHelper(root);
Console.WriteLine(current.Data);
}