X32专项练习部分19
递归树
class Test {
public static int count = 0;
public static void main(String[] args) {
int i;
i = x(8);
System.out.println(count); // 9
}
public static int x(int n) {
count++;
if (n <= 3)
return 1;
else
return x(n - 2) + x(n - 4) + 1;
}
/*
count的作用是统计递归次数
共有几个递归程序进入x方法体
以8为根节点画出递归树
8个分支表明调用8次
再加上自身调用x(8)一次
共9次
8
6 4
4 2 2 0