Java 如何停止递归

Java 如何停止递归,java,Java,下面是我的find()方法: 我试图找出一个给定的数是否等于二叉搜索树中任意两个数的和。我知道错误在哪里,但我不知道如何纠正它 翼线 if(find(result)) { return true; } 应该终止该方法,因为一旦我得到一个true,这就是我所需要的,我应该将该值返回给调用函数。但是,递归将继续执行,最终返回上次递归调用的值。请帮忙 public boolean searchNum(BinTreeNode node, int num) { if(node == nul

下面是我的find()方法:

我试图找出一个给定的数是否等于二叉搜索树中任意两个数的和。我知道错误在哪里,但我不知道如何纠正它

翼线

if(find(result)) {

return true; 

}
应该终止该方法,因为一旦我得到一个true,这就是我所需要的,我应该将该值返回给调用函数。但是,递归将继续执行,最终返回上次递归调用的值。请帮忙

public boolean searchNum(BinTreeNode node, int num) {
    if(node == null) {
        return false;
    }
    else {
        if(node.leftChild != null) {
            searchNum(node.leftChild, num);
        }
        int result = num - node.item;
        System.out.println(node.item);
        //I have a separate find() which finds if the key is in the tree
        if(find(result)) {
            return true; 
        }

       if(node.rightChild != null) {
            searchNum(node.rightChild, num);
        } 

       return false;
    }
}
}

在递归检查树的左侧之前,请尝试移动
find
检查。否则,在检查节点本身之前,您将先查看每个节点的左侧

else {
    // Moved to the top of the else
    int result = num - node.item;
    System.out.println(node.item);
    if(find(result)) {
        return true; 
    }
    // Then check recursively
    if(node.leftChild != null) {
        searchNum(node.leftChild, num);
    }
    if(node.rightChild != null) {
        searchNum(node.rightChild, num);
    } 

   return false;
}

最通用的递归公式如下所示:

function sampleRecursion(a){
    if(isTerminalCase) {
        return true;
    }
    else {
       a = modify(a);
       return sampleRecursion(a);
    }
}
将其用作代码的模板,您应该有如下内容:

public boolean searchNum(BinTreeNode node, int num) {
    //validate the input
    if(node == null) {
        return false;
    }
    //this is your terminal case
    int result = num - node.item;
    //I have a separate find() which finds if the key is in the tree
    if(find(result)) {
        return true; 
    }
    //this if doesn't make any sense as you validate this input already
    //if(node.leftChild != null) {
    //    searchNum(node.leftChild, num);
    //}
    //here is where we are returning the value we get back from searchNum
    return seachNum(node.leftChild, num) || searchNum(node.rightChilde, num);
    //System.out.println(node.item);
    //I moved this up into the single return case, so everything after this is unnecessary
    //if(node.rightChild != null) {
    //    searchNum(node.rightChild, num);
    //} 
    //return false;
}
所以,清理干净后,应该是这样的:

public boolean searchNum(BinTreeNode node, int num) {
    //validate the input
    if(node == null) {
        return false;
    }
    //this is your terminal case
    int result = num - node.item;
    //I have a separate find() which finds if the key is in the tree
    if(find(result)) {
        return true; 
    }
    //here is where we are returning the value we get back from searchNum
    return seachNum(node.leftChild, num) || searchNum(node.rightChilde, num);
}
这是递归函数的近似形式。我不知道find函数是做什么的,所以我不知道它是否符合您的要求

此外,根据您的描述,您试图在搜索树中使用两个数字,而不是一个。在您现在拥有的代码中,您只使用了一个。所以如果你传入,比如说,({a node},15),你会得到。。。某物如果find函数调用searchNum({top node},result),我想您会没事的。但我不知道find做了什么,所以我不能肯定

编辑:合并
find
findHelper
可能更简单

private boolean find(int key, BinTreeNode node) {
    if (node == null) {
        return false;
    }    
    if(key == node.item) {
        return true;
    }
    else if(key < node.item) {
        return findHelper(key,node.leftChild);
    }
    else {
        return findHelper(key,node.rightChild);
    }
}
私有布尔查找(int键,BinTreeNode节点){
if(node==null){
返回false;
}    
if(key==node.item){
返回true;
}
else if(键<节点项){
返回findHelper(键,node.leftChild);
}
否则{
return findHelper(key,node.rightChild);
}
}

然后用
find(result,root)
searchNum
中调用它。也就是说,如果您有权在
searchNum
中访问root

您已经给出了一些代码,其中一部分是另一个关键函数。它需要一段查找代码,可能你对searchNum什么都没做。您需要在堆栈中返回searchNum的值,否则就会丢失该值。现在,在一个非终端的情况下,唯一可能的返回值是false。我如何将该值传递给递归树?我迷路了:(见我的答案。你只需返回值。递归实际上不是一棵树,它是一个堆栈。哦,好吧!我刚刚为树的顺序遍历编写了代码。那么,我应该写预顺序遍历吗?是的。当你找到你要查找的内容时,你想停止,那么为什么还要进一步搜索呢?我只是移动了我的查找,但它仍然不起作用:(如何再次粘贴我的代码?我只能“注释”而不能“发布”回复。非常感谢。我不理解那个注释。为什么那个块没有意义?在函数开始时,你说
if(node==null)
。那么,没有理由说
if(node.leftChild!=null)
在传入该值之前,因为您已经在
searchNum
的最开始检查null了。哦,我明白了。所以我的错误是在“searchNum(this.leftChild)”之前省略了“return”。我现在无法理解它是如何使我的代码工作的!请帮助我理解它。顺便说一下,我已经编辑了我的原始帖子以包含我的find()。请看一看。如果我不检查null,并且如果this.leftChild结果为null,我不会得到NullPointerException吗?我正在开始检查,但我不会在这里得到异常吗?如果您尝试对null执行某些操作(任何尝试取消引用它的操作),您只会得到null指针异常。只要在取消引用指针之前进行检查,就可以了。因此,即使
node.leftChild
为null,也可以,因为传入时,您将看到它为null并返回false。
public boolean searchNum(BinTreeNode node, int num) {
    //validate the input
    if(node == null) {
        return false;
    }
    //this is your terminal case
    int result = num - node.item;
    //I have a separate find() which finds if the key is in the tree
    if(find(result)) {
        return true; 
    }
    //here is where we are returning the value we get back from searchNum
    return seachNum(node.leftChild, num) || searchNum(node.rightChilde, num);
}
private boolean find(int key, BinTreeNode node) {
    if (node == null) {
        return false;
    }    
    if(key == node.item) {
        return true;
    }
    else if(key < node.item) {
        return findHelper(key,node.leftChild);
    }
    else {
        return findHelper(key,node.rightChild);
    }
}