Pushback new vector<int>() to your return 2d Vector as new level is reached with this code


#1

vector<vector > Solution::levelOrder(TreeNode* A) {

vector<vector<int>> res;
queue<pair<TreeNode*, int>> q;

q.push({A,1});

while(!q.empty())
{
    TreeNode* temp = q.front().first;
    int k = q.front().second;
    q.pop();
    
    if(temp->left!=NULL) q.push({temp->left, k+1});
    if(temp->right!=NULL) q.push({temp->right, k+1});
    
    if(res.size()<k) res.push_back(vector<int>());
    res[k-1].push_back(temp->val);
}
return res;

}