题目如下:
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree {3,9,20,#,#,15,7},
3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:
[ [3], [20,9], [15,7] ]confused what "{1,#,2,3}" means?
题目分析:
在前面1题的基础上,本题增加了一个小变化,就是输出的时候,一行从左到右输出,另一行从右到左输出。于是可以设定flag来代表奇偶的变化。
代码如下:
//36ms过大集合
/**
* Definition for binary tree
* 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_vec_vec;
if(root==NULL)
return res_vec_vec;
queue<queue<TreeNode*>> que_que;
queue<TreeNode*> que;
que.push(root);
que_que.push(que);
int flag=1;
while(!que_que.empty()){
queue<TreeNode*> tmp_src_queue=que_que.front();
queue<TreeNode*> tmp_out_queue;
vector<int> tmp_vec;
while(!tmp_src_queue.empty()) {
tmp_vec.push_back(tmp_src_queue.front()->val);
TreeNode* tmp_node=tmp_src_queue.front();
if(tmp_node->left!=NULL)
tmp_out_queue.push(tmp_node->left);
if(tmp_node->right!=NULL)
tmp_out_queue.push(tmp_node->right);
tmp_src_queue.pop();
}
if(flag>0) {
res_vec_vec.push_back(tmp_vec);
}else{
int k=0;
int h=tmp_vec.size()-1;
while(k<h){
int tmp=tmp_vec[k];
tmp_vec[k]=tmp_vec[h];
tmp_vec[h]=tmp;
k++;
h--;
}
res_vec_vec.push_back(tmp_vec);
}
flag=0-flag;
if(!tmp_out_queue.empty())//如果没有这个条件判断,程序就死循环了
que_que.push(tmp_out_queue);
que_que.pop();
}
return res_vec_vec;
}
};
update: 2014-12-03
/**
* Definition for binary tree
* 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) {
TreeNode* current;
queue<TreeNode*> que;
queue<TreeNode*> temp_que;
vector<int> vec;
vector<vector<int> > nested_vec;
if(root != NULL) que.push(root);
bool is_odd = true;
while(!que.empty()) {
current = que.front();
if(is_odd) {
vec.push_back(current->val);
}else {
vec.insert(vec.begin(),current->val); //NOTE: insert(参数1,参数2), 其中参数1不能用数字表示位置 ×: vec.insert(0,current->val);
}
que.pop();
if (current->left != NULL)
temp_que.push(current->left);
if (current->right != NULL)
temp_que.push(current->right);
if (que.size()==0) {
is_odd = 1 - is_odd;
que = temp_que;
nested_vec.push_back(vec);
vec.clear();
queue<TreeNode*> q;
temp_que.swap(q);//NOTE: std::queue没有clear()函数,可以swap一个空queue相当于清零。
}
}
return nested_vec;
}
};
update: 2015-03-24
1. 做了一个小改进,参照前一题目,使用size来表示每层的节点个数,简化代码。其它地方基本差不多。
//10ms
/**
* Definition for binary tree
* 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) {
queue<TreeNode*> q;
if (root != NULL) {
q.push(root);
}
vector<int> inner;
vector<vector<int> > outer;
TreeNode* node;
bool isReversed = false;
while(!q.empty()) {
int size = q.size();
inner.clear();
while (size-- > 0) {
node = q.front();
q.pop();
inner.push_back(node->val);
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right);
}
if(isReversed) {
reverse(inner.begin(), inner.end());
}
outer.push_back(inner);
isReversed = 1 - isReversed;
}
return outer;
}
};