-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path103-二叉树的锯齿层次遍历.cpp
More file actions
40 lines (40 loc) · 1.04 KB
/
103-二叉树的锯齿层次遍历.cpp
File metadata and controls
40 lines (40 loc) · 1.04 KB
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>> answer;
if(root == NULL)
return answer;
queue<TreeNode*> que;
que.push(root);
int flag = 0;
while(!que.empty())
{
int len = que.size();
vector<int> ans_temp;
while(len--)
{
TreeNode* temp = que.front();
que.pop();
ans_temp.push_back(temp->val);
if(temp->left!=NULL)
que.push(temp->left);
if(temp->right!=NULL)
que.push(temp->right);
}
if(flag)
reverse(ans_temp.begin(),ans_temp.end());
flag = !flag;
answer.push_back(ans_temp);
}
return answer;
}
};