/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int> >ret;
queue<TreeNode*>layer;
bool left2right = true;
if(nullptr != root)
layer.push(root);
while(!layer.empty())
{
queue<TreeNode*>layertmp;
vector<int>outvec;
swap(layertmp, layer);
while(!layertmp.empty())
{
if(nullptr != layertmp.front()->left)
layer.push(layertmp.front()->left);
if(nullptr != layertmp.front()->right)
layer.push(layertmp.front()->right);
outvec.push_back(layertmp.front()->val);
layertmp.pop();
}
if(!left2right)
reverse(outvec.begin(), outvec.end());
left2right = !left2right;
ret.push_back(outvec);
}
return ret;
}
};