-
Notifications
You must be signed in to change notification settings - Fork 1
/
103.cpp
54 lines (50 loc) · 1.16 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>>A;
if(root==NULL){
return A;
}
stack<TreeNode*>q1;
stack<TreeNode*>q2;
q1.push(root);
while(!q1.empty())
{
vector<int>B;
int sz1=q1.size();
while(sz1--){
TreeNode * node =q1.top();
q1.pop();
B.push_back(node->val);
if(node->left){
q2.push(node->left);
}
if(node->right){
q2.push(node->right);
}
}
if(B.size()!=0){
A.push_back(B);
}
B.clear();
int sz2=q2.size();
while(sz2--){
TreeNode * node =q2.top();
q2.pop();
B.push_back(node->val);
if(node->right)
{
q1.push(node->right);
}
if(node->left)
{
q1.push(node->left);
}
}
if(B.size()!=0){
A.push_back(B);
}
}
return A;
}
};