描述给定一个二叉树,返回该二叉树的之字形层序遍历,(第一层从左向右,下一层从右向左,一直这样交替)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/89d8e71af1ebb2454d7d1f48508b9b26.png)
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int>> res;
if(pRoot==NULL)
return res;
queue<TreeNode*>q;
q.push(pRoot);
vector<int>tem;
tem.push_back(pRoot->val);
res.push_back(tem);
int flag = 1;
while(!q.empty()){
vector<int>temvec;
int size= q.size();
for(int i=0;i<size;i++){
TreeNode* tem = q.front();
if(tem->left!=NULL){
temvec.push_back(tem->left->val);
q.push(tem->left);
}
if(tem->right!=NULL)
{
temvec.push_back(tem->right->val);
q.push(tem->right);
}
q.pop();
}
flag++;
if(flag%2==0)
{
int m = temvec.size()/2;
for(int i=0;i<m;i++)
{
int tem = temvec[i];
temvec[i] = temvec[temvec.size()-1-i];
temvec[temvec.size()-1-i]=tem;
}
}
res.push_back(temvec);
}
vector<vector<int>>::iterator it = res.end();
res.erase(it-1, it);
return res;
}
};