class Solution { public: // 考察知识点: 二叉树的层次遍历 队列的先入先出性质 vector<vector<int> > Print(TreeNode* pRoot) { queue<TreeNode*> q; vector<vector<int>> vv; if(pRoot == NULL) return vv; q.push(pRoot); while(!q.empty()) { int n = q.size(); // 每一层结点的个数 vector<int> v; for(int i=0; i<n; i++) { TreeNode* node = q.front(); q.pop(); v.push_back(node->val); // 用于存储每一层的结点的val cout << node->val << " "; if(node->left != NULL) { q.push(node->left); } if(node->right != NULL) { q.push(node->right); } } cout << endl; vv.push_back(v); } return vv; } };