/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型vector<vector<>>
*/
vector<vector<int> > levelOrder(TreeNode* root) {
vector<vector<int> > resVec;
if(root == nullptr){
return resVec;
}
/*
1.定义一个队列
2.往队列里加入当前节点
3.当队列不为空时:
a.求队列长度
b.从队列前面取节点,判断当前节点有无子树,有则入队
4.重复操作,返回数据
*/
queue<TreeNode *> myQue;
myQue.push(root);
while(!myQue.empty()){
vector<int> temp;
int length = myQue.size();
for(int index = 0;index < length;index++){
TreeNode *tNode = myQue.front();
temp.push_back(tNode->val);
myQue.pop();
if(tNode->left != nullptr){
myQue.push(tNode->left);
}
if(tNode->right != nullptr){
myQue.push(tNode->right);
}
}
resVec.push_back(temp);
}
return resVec;
}
};