求二叉排序树的最小根

来源:互联网 发布:阿里云域名使用方法 编辑:程序博客网 时间:2024/05/29 08:03
public class Solve{
public static void main(String[] args){
int aa=minRoot(4,10,13);
int bb=minRoot(4,aa,15);
System.out.println(bb);
}
public static int minRoot(int K,int a,int b){
if(a<b){
int tmp=a;
a=b;
b=tmp;
}
if (a > Math.pow(2, K) - 1 || b < 0)
return -1;

if (a >= Math.pow(2, K - 1) && b <= Math.pow(2, K - 1)) {
return (int) Math.pow(2, K - 1);
}

if (b > Math.pow(2, K - 1)) {
return (int)(Math.pow(2, K - 1)+minRoot(K-1,(int)(a-Math.pow(2, K - 1))
,(int)(b-Math.pow(2, K - 1))));
}

return minRoot(K-1,a,b);
}
}
0 0
原创粉丝点击