Easy BFS of Binary Trees


#1
vector<vector<int> > Solution::levelOrder(TreeNode* root)
{
        if(!root)
        {
            return {};
        }
        vector<vector<int>>v;
        queue<TreeNode *>q;
        q.push(root);
        while(!q.empty())
        {
            int len=q.size();
            vector<int>aux;
            while(len)
            {
                TreeNode *node=q.front();
                aux.push_back(node->val);
                q.pop();
                if(node->left)
                {
                    q.push(node->left);
                }
                if(node->right)
                {
                    q.push(node->right);
                }
                len--;
            }
            v.push_back(aux);
        }
        return v;
}