Posted on

Description

Submission

class Solution {
public:
    vector<int> pathInZigZagTree(int label) {
        if(label == 1) return {1};

        int l = label;
        int n = floor(log2(label))+1;
        int low = pow(2, n-1);
        int high = pow(2, n) - 1;
        if(n & 1) {
            l = low + high - l;
        }
        
        vector<int> rets(n);
        for(; n > 0 ; --n, l /= 2) {
            int low = pow(2, n-1);
            int high = pow(2, n) - 1;
            if(n & 1) {     // odd, reversed
                rets[n-1] = low + high - l;
            } else {
                rets[n-1] = l;
            }
        }

        return rets;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *