Java中有没有办法在满足某个条件时结束递归方法?

发布于 2024-11-05 05:13:19 字数 543 浏览 6 评论 0原文

有没有办法在满足条件时突然返回到调用下面方法的方法?类似于循环的中断。

public void createTree(TreeNode tree) throws IOException {
    if (i >= preorder.length) {
        // Leave recursive method
    } else if (preorder[i] == '0') {
        tree.value = '0';
        tree.left = tree.right = null;
        i++;                
    } else {
        tree.value = preorder[i];
        i++;
        tree.left = new TreeNode();
        createTree(tree.left);
        tree.right = new TreeNode();
        createTree(tree.right);
    }
}

谢谢。

Is there a way to abruptly return to the method that calls the method below when the condition is met? Something similar to a break for loops.

public void createTree(TreeNode tree) throws IOException {
    if (i >= preorder.length) {
        // Leave recursive method
    } else if (preorder[i] == '0') {
        tree.value = '0';
        tree.left = tree.right = null;
        i++;                
    } else {
        tree.value = preorder[i];
        i++;
        tree.left = new TreeNode();
        createTree(tree.left);
        tree.right = new TreeNode();
        createTree(tree.right);
    }
}

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

执笏见 2024-11-12 05:13:19

您可以简单地让该方法返回。然后递归将向后执行,并执行剩余的语句。

You can simply let the method return. Then the recursion will go backwards and the remaining statements will be executed.

陌若浮生 2024-11-12 05:13:19

您可以从该方法返回状态:

/**
 * @return false if the recursion has been aborted
 */
public boolean createTree(TreeNode tree) throws IOException {
    if (i >= preorder.length) {
        return false;
    } 
    else if (preorder[i] == '0') {
        tree.value = '0';
        tree.left = tree.right = null;
        i++;                
    } 
    else {
        tree.value = preorder[i];
        i++;
        tree.left = new TreeNode();
        if (!createTree(tree.left)) {
            return false;
        };
        tree.right = new TreeNode();
        if (!createTree(tree.right)) {
            return false;
        }
    }
}

You could return a status from the method :

/**
 * @return false if the recursion has been aborted
 */
public boolean createTree(TreeNode tree) throws IOException {
    if (i >= preorder.length) {
        return false;
    } 
    else if (preorder[i] == '0') {
        tree.value = '0';
        tree.left = tree.right = null;
        i++;                
    } 
    else {
        tree.value = preorder[i];
        i++;
        tree.left = new TreeNode();
        if (!createTree(tree.left)) {
            return false;
        };
        tree.right = new TreeNode();
        if (!createTree(tree.right)) {
            return false;
        }
    }
}
痴情 2024-11-12 05:13:19

你可以抛出异常,但这似乎是“肮脏的”

you could throw an exception, but this seems to be "dirty"

冰雪之触 2024-11-12 05:13:19

您可能会考虑使用运行时异常,但我认为这不是一个好的设计。

如果我的递归走那么远,则意味着它是必需的......并且我可能会使用返回

You might consider using an runtimeexception but i do not think it is a good design.

If my recursion went that far it means that it was required... and i will probably used a return

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文