forked from gh877916059/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path071. 二叉树的锯齿形层次遍历.cpp
43 lines (43 loc) · 1.05 KB
/
071. 二叉树的锯齿形层次遍历.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
class Solution
{
public:
vector<vector<int>> zigzagLevelOrder(TreeNode *root)
{
vector<vector<int> > result;
vector<int> cur;
if(root==NULL)
return result;
queue<TreeNode*> Q;
int counter1=1;
int counter2=0;
Q.push(root);
while(!Q.empty())
{
while(counter1-->0)
{
TreeNode* temp=Q.front();
cur.push_back(temp->val);
Q.pop();
if(temp->left!=NULL)
{
Q.push(temp->left);
++counter2;
}
if(temp->right!=NULL)
{
Q.push(temp->right);
++counter2;
}
}
result.push_back(cur);
cur.clear();
counter1=counter2;
counter2=0;
}
for(int i=1;i<result.size();i=i+2)
{
reverse(result[i].begin(),result[i].end());
}
return result;
}
};