class Solution {

public:

vector<vector<int> > zigzagLevelOrder(TreeNode *root) {

vector<vector<int>> result;

if(root==nullptr) return result;

vector<int> layer;

stack<TreeNode *> current, next;

bool leftToRight=true;

current.push(root);

while(!current.empty()){

TreeNode *tmp = current.top();

```
current.pop();
if(tmp){
layer.push_back(tmp->val);
if(leftToRight){
next.push(tmp->left);
next.push(tmp->right);
}else{
next.push(tmp->right);
next.push(tmp->left);
}
}else{
if(current.empty()){
result.push_back(layer);
leftToRight=!leftToRight;
swap(current, next);
}
}
}
return result;
}
```

};