class Solution {
public:
// this is math problem
// [1] [3,2] [4,5,6,7] [15,14,13,12,11,10,9,8]
// 14 -> [4-th 14-8+1=7 nodes -> 3-th ceil(7/2)=4 nodes, 8-4=4]->3-th row
// 4 -> [3-th, 4-4+1=1; ceil]
vector<int> pathInZigZagTree(int label) {
int row = floor(log(label)/log(2))+1; //1 层
cout<<row<<endl; //4
vector<int> res(row, 0); // [0]
res[row-1] = label; // [1]
res[0] = 1; // [1]
for(int j=row-2;j>=0;j--){ //j=2:1,0
// j=2: 8-3=5
res[j] = pow(2, j+1) - ceil((res[j+1]-pow(2, j+1)+1)/2.0);
}
return res;
}
};
1104. Path In Zigzag Labelled Binary Tree
猜你喜欢
转载自blog.csdn.net/zeroQiaoba/article/details/104722519
今日推荐
周排行