【发布时间】:2016-10-31 06:40:59
【问题描述】:
嗨。我无法以递归格式编写此方法(在照片中)。该方法获取给定元素在二叉搜索树中的出现次数。 为了递归地解决这个问题,我尝试使用同名的私有辅助方法来实现它,如下所示:
public int count(){
count = 0;
if (root == null)
return count;
return count (root.getInfo());
private int count(T element){
(Basically the same code you see in the photo)
}
但我最终遇到了溢出错误。你介意看看并告诉我如何递归地构造这个方法吗?
干杯,谢谢。
【问题讨论】:
-
root 不是函数的局部变量,这可能是错误的原因。您想要一个递归函数,但您使用的循环没有意义,并且 if 条件中的“root = root.getLeft()”也没有意义。
标签: recursion tree binary-search-tree