You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

含递归调用的循环方法时间复杂度计算咨询

嵌套递归与循环的时间复杂度分析问题

我正在计算嵌套在普通while循环中的递归方法的时间复杂度,目前无法确定正确解答。初步认为答案应为O(N*Y),其中Y代表递归涉及的树的相关参数。

用于统计大于x的叶子节点数量的递归树方法如下:

public static int bigger(BinaryNode<Integer> t, int x)
{
    if(t==null)
    {
        return 0;
    }
    if (t.getValue()>x) {
        return 1;
    }
    return bigger(t.getLeft(),x) + bigger(t.getRight(),x);
}

我需要计算时间复杂度的是以下方法(每次循环迭代都会调用bigger()):

public static Stack<Item> bigInTree(BinaryNode<Integer> bt,Stack<Integer> s) {
    Stack<Item> n = new Stack<>();

    while (!s.isEmpty())
    {
        n.push(new Item(s.top(),bigger(bt,s.top())));
        s.pop();
    }

    return n;
}

请问该方法的时间复杂度是O(N²)还是可以直接表示为O(N*Y)(Y为树t的叶子节点数量)?

内容的提问来源于stack exchange,提问作者itamar nahum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 08:36:19