/**
* Note: The returned array must be malloced, assume caller calls free().
*/
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;
}
04-29
390
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-28
144
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-03
271
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-31
09-03
643
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-02
825
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)