```
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> result;
if(root == nullptr) return result; //edge case
queue<TreeNode*> myqueue;
myqueue.push(root);
int order = 1; //1 means from left to right, -1 means from right to left
int currLevel = 1, nextLevel = 0;
vector<int> vec;
while(!myqueue.empty()){
TreeNode* curr = myqueue.front();
myqueue.pop();
if(curr->left){
myqueue.push(curr->left);
nextLevel++;
}
if(curr->right){
myqueue.push(curr->right);
nextLevel++;
}
currLevel--;
if(order == 1) vec.push_back(curr->val);
else if(order == -1) vec.insert(vec.begin(), curr->val);
if(currLevel == 0){
result.push_back(vec);
vec.clear();
currLevel = nextLevel;
nextLevel = 0;
order = 0 - order;
}
}
return result;
}
};
```