下面是 Scala 中的一个程序。
def range(low : Int, high : Int) : List[Int] = {
var result : List[Int] = Nil
result = rangerec(root, result, low, high)
result
}
private def rangerec(r : Node, l : List[Int], low : Int, high :Int) : List[Int] = {
var resultList : List[Int] = List()
if(r.left != null) {
rangerec(r.left, resultList, low, high)
} else if(r.right != null) {
rangerec(r.right, resultList, low, high)
} else {
if(r.key >= low && r.key <= high) {
resultList = resultList ::: List(r.key)
resultList
}
}
resultList
}
我在我的二叉搜索树中创建了 range 方法,实现了中序遍历算法。所以它必须递归地工作,但它不打印任何东西,List()。如何修复我的算法?或编辑我的代码?