https://www.nowcoder.com/practice/445c44d982d04483b04a54f298796288?
思路:使用两个数组分别保存当前层和下一层的结点
vector<vector<int>> Print(TreeNode *pRoot) {
vector<vector<int>> ans;
vector<TreeNode *> root; //存放当前层的所有根结点
vector<TreeNode *> child; //存放当前层的所有孩子结点
if (pRoot){
root.push_back(pRoot);
while (!root.empty()) {
vector<int> temp; //存放当前层结点的值
for (auto r: root) {
if (r->left) child.push_back(r->left); //存入左孩子
if (r->right) child.push_back(r->right); //存入右孩子
temp.push_back(r->val);
}
ans.push_back(temp);
root = child; //孩子结点将作为下次循环的根结点
child.clear(); //清空孩子结点
}
}
return ans;
}