参考模版:
BFS是很经典的模板,模板在手,天下我有。
模板的精髓
1.使用队列来确保每层的先后顺序不发生变化
2.遍历的时候取出队列的size,确保每次只遍历一层。

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
        vector<vector<int> > Print(TreeNode* pRoot) {
            vector<vector<int> > res;
            queue<TreeNode*> p;
            if(pRoot){
                p.push(pRoot);
            }
            while(!p.empty()){
                int size = p.size();
                vector<int> current = {};
                while(size--){
                    TreeNode *node = p.front();
                    p.pop();
                    current.push_back(node->val);
                    if(node->left){
                        p.push(node->left);
                    }
                    if(node->right){
                        p.push(node->right);
                    }
                }
                res.push_back(current);
            }
            
            return res;
        }
    
};