Easy solution using a Queue


#1

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

vector<vector<int>>res;


queue<TreeNode *>q;
TreeNode * point =A;
q.push(A);
vector <int>v;
while(q.empty()==false)
{
    
    TreeNode * top=q.front();
    q.pop();
    v.push_back(top->val);
       
    if(top->left!=NULL)
    {
       
        q.push(top->left);
       
    }
    if(top->right!=NULL)
    {
        
        q.push(top->right);
    }
    if(point==top)
    {
        // This step is to separate the rows i.e levels.
        point=q.back();
        res.push_back(v);
        v.clear();
    }
    
}
return res;

}


#2

this is how i did