-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLC_103.cpp
40 lines (40 loc) · 932 Bytes
/
LC_103.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
/**
* 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;
if(root==NULL)
return res;
queue<TreeNode*>q;
int i,count,iter=0;
q.push(root);
while(!q.empty())
{
count=q.size();
vector<int>v;
iter++;
for(i=0;i<count;i++)
{
TreeNode *x=q.front();
q.pop();
v.push_back(x->val);
if(x->left)
q.push(x->left);
if(x->right)
q.push(x->right);
}
if(iter%2==0)
reverse(v.begin(),v.end());
res.push_back(v);
}
return res;
}
};