-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Tree Zigzag Level Order Traversal.cpp
47 lines (46 loc) · 1.32 KB
/
Binary Tree Zigzag Level Order Traversal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
struct node{
TreeNode * p;
int l;
node():p(NULL),l(0){}
node(TreeNode * x,int ll):p(x),l(ll){}
};
class Solution {
public:
vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
vector<vector<int> > ans;
if(root==NULL) return ans;
queue<node> q;
node tmp;
q.push(node(root,0));
int f;
while(!q.empty()){
tmp = q.front();
q.pop();
if(tmp.l == 0){
ans.push_back(vector<int>());
}
else{
if( tmp.l!=f ){
if( tmp.l%2==0 ){
int len=ans[tmp.l-1].size();
for(int i=0;i<len/2;i++){
swap(ans[tmp.l-1][i],ans[tmp.l-1][len-1-i]);
}
}
ans.push_back(vector<int>());
}
}
f=tmp.l;
ans[tmp.l].push_back(tmp.p->val);
if(tmp.p->left != NULL) q.push( node(tmp.p->left,tmp.l+1) );
if(tmp.p->right!= NULL) q.push( node(tmp.p->right,tmp.l+1) );
}
if( tmp.l%2==1 ){
int len=ans[tmp.l].size();
for(int i=0;i<len/2;i++){
swap(ans[tmp.l][i],ans[tmp.l][len-1-i]);
}
}
return ans;
}
};