int pow1(int x){
int sum=1;
for(int a=0;a<x;a++){
sum=sum*2;
}
return sum;
}
int* pathInZigZagTree(int label, int* returnSize){
int count=0;
int sum=1;
*returnSize=0;
int* ans=(int*)calloc(10000,sizeof(int));
while(1){
count++;
if(pow1(count-1)<=label&&pow1(count)>label){
break;
}
}
ans[*returnSize]=label;
*returnSize=*returnSize+1;
while(count=count-1){
int right=pow1(count)-1;
int left=pow1(count-1);
if(label%2==0){
label=label/2;
}
else{
label=(label-1)/2;
}
label=right-label>label-left?right-label+left:left+right-label;
if(label==0){
break;
}
ans[*returnSize]=label;
*returnSize=*returnSize+1;
}
for(int a=0;a<*returnSize-1;a++){
for(int b=a+1;b<*returnSize;b++){
if(ans[a]>ans[b]){
int c=ans[a];
ans[a]=ans[b];
ans[b]=c;
}
}
}
return ans;
}
|