Simple C++ solution (Recursive)


#1
# define ll long long 
void helper(int A, vector<int> &res, int size, int num){
    if(num == size) return; //base
    
    int pow = num/2;
    res.push_back(num^pow);
    helper(A, res, size, num+1);
}

vector<int> Solution::grayCode(int A) {
    vector<int> res;
    ll size = pow(2, A);
    helper(A, res, size, 0);
    return res;
    
}