v.insert(v.begin(), value);
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> res;
levelOrder(res, root);
return res;
}
private:
void levelOrder(vector<vector<int>>& res, TreeNode* root) {
if (!root) {
return;
}
queue<TreeNode*> q;
q.push(root);
int cnt = 1;
while (!q.empty()) {
int size = q.size();
vector<int> record;
for (int i = 0; i < size; i++) {
TreeNode* front;
front = q.front();
q.pop();
if (cnt % 2) {
record.push_back(front->val);
} else {
record.insert(record.begin(), front->val);
}
if (front->left) {
q.push(front->left);
}
if (front->right) {
q.push(front->right);
}
}
cnt++;
res.push_back(record);
}
}
};