腾讯笔试完全二叉排序树
大家看一下我的解法,有没有问题,可以一起讨论一下~大致的思路就是找到最大数和最小数之间的2的最大幂次的倍数。
从最大的2^(k-1)次幂开始除起,看在哪一个幂次时max/2^i不等于min/2^i,就说明公共根节点时2^i的倍数
// tencent_BST.cpp : Defines the entry point for the console
application.
//
#include<stdio.h>
#include<math.h>
int main()
{
int k, a, b, c;
scanf("%d %d %d %d",&k,&a,&b,&c);
int i = k - 1;
int tempmax = a>b ? a : b;
int max = tempmax > c ? tempmax : c;
int tempmin = a < b ? a : b;
int min = tempmin < c ? tempmin : c;//求最大最小值
while (i >= 0)
{
int tempa = max / (int)pow(2, i);
int tempb = min / (int)pow(2, i);
if (tempa == tempb)
i--;
else
break;
}//找到根节点是2^i的倍数
printf("%d\n",(int)((max/(int)pow(2,i))*pow(2,i)));
return 0;
}
阿里云成长空间 741人发布