CPP SIngle Queue


#1
vector<vector<int> > Solution::levelOrder(TreeNode* a) {
    queue<TreeNode *>q;
    vector<vector<int>> ans;
    if(!a) return ans;
    q.push(a);
    q.push(NULL);
    vector<int> curr;
    while(!q.empty())
    {
        TreeNode *temp = q.front();
        q.pop();
        if(temp)
        {
            curr.push_back(temp->val);
            if(temp->left) q.push(temp->left);
            if(temp->right) q.push(temp->right);
        }
        else
        {
            ans.push_back(curr);
            curr.clear();
            if(q.empty()) break;
            q.push(NULL);
        }
    }
    return ans;
}