Easy solution using queue


#1

/**

  • Definition for binary tree

  • struct TreeNode {

  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    /
    vector<vector > Solution::levelOrder(TreeNode
    A)
    {
    vector<vector>ans;
    vectorv;
    if(A==NULL)
    {
    return ans;
    }
    if(A->left==NULL&&A->right==NULL)
    {
    v.push_back(A->val);
    ans.push_back(v);
    return ans;
    }

    queue<TreeNode *>q;
    TreeNode *temp=new TreeNode(-5);
    q.push(A);
    while(!q.empty())
    {
    q.push(temp);
    while(q.front()!=temp)
    {
    TreeNode *list=q.front();
    if(list->left!=NULL)
    {
    q.push(list->left);
    }
    if(list->right!=NULL)
    {
    q.push(list->right);
    }
    v.push_back(list->val);
    q.pop();
    }
    ans.push_back(v);
    v.clear();
    q.pop();

    }
    return ans;

}