Without map, without reverse


#1

vector Solution::solve(TreeNode* A) {
queue<pair<int, TreeNode*>> q;
int x = 0;
vector ans;
vector subv;
q.push({0, A});
while(!q.empty()){
TreeNode* n = q.front().second;
int level = q.front().first;
q.pop();
if(level>x){
x++;
ans.insert(ans.begin(), subv.begin(), subv.end());
subv.clear();
}
if(n->left){
q.push({level+1, n->left});
}
if(n->right){
q.push({level+1, n->right});
}
if(level==x){
subv.push_back(n->val);
}
}
ans.insert(ans.begin(), subv.begin(), subv.end());
return ans;
}