原题链接:103. 二叉树的锯齿形层序遍历
solution:
层序遍历,加一个reverse
class Solution {
public:
vector> zigzagLevelOrder(TreeNode* root) {
vector> res; //定义返回值
queue que;
if(root != nullptr) que.push(root);
bool flag = false;
while(!que.empty()) {
int size = que.size();
vector path;
while(size--) {
auto t = que.front();
que.pop();
path.push_back(t->val);
if(t->left != nullptr) que.push(t->left);
if(t->right != nullptr) que.push(t->right);
}
if(flag) reverse(path.begin(),path.end());
flag = !flag;
res.push_back(path);
}
return res;
}
};



