返回最低键和最高键之间的差异 - 二叉搜索树
这是我正在尝试的关于二叉搜索树的过去考试试卷。我无法检查输出是否正确,因为我无法构建其中之一。
问题在标题中,
class Tree{
Tree left;
Tree right;
int key;
public static int span(Tree tree)
{
if ( tree == null ){
return null;
}
if( tree.left != null)
int min = span(tree.left);
}
if( tree.right != null){
int max = span(tree.right);
}
return max - min;
}
}
有人可以建议我需要更改什么才能获得 5/5 分吗:D - 我们唯一要做的就是编写 span
方法,标题已为我们提供。
This is a past exam paper on binary search trees I am attempting. I have no way to check if the output is correct as I am not capable of building one of these things.
The question is in the title
class Tree{
Tree left;
Tree right;
int key;
public static int span(Tree tree)
{
if ( tree == null ){
return null;
}
if( tree.left != null)
int min = span(tree.left);
}
if( tree.right != null){
int max = span(tree.right);
}
return max - min;
}
}
Could anyone suggest what I need to change to get 5/5 marks :D - the only thing we have to do is write the span
method, the header was given for us.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要定义两个方法,
min(Tree)
和max(Tree)
,然后span(Tree t)
定义为最大值(t)-最小值(t)
。span
本身不应该是递归的,但如果需要,您可以使min
和max
递归。请注意,
min
和max
没有是它们自己的方法。如果您不介意让它们作为自己的单位,您可以将其全部放入span
中,如下所示:You need to define two methods,
min(Tree)
andmax(Tree)
, thenspan(Tree t)
is defined asmax(t) - min(t)
.span
itself shouldn't be recursive, but you can makemin
andmax
recursive if you want.Note that
min
andmax
doesn't have to be their own methods. If you don't care for making them stand as their own units, you can put it all intospan
like this: