Skip to content

Commit 24d8dea

Browse files
authored
Create 103. Binary Tree Zigzag Level Order Traversal
103. Binary Tree Zigzag Level Order Traversal
1 parent 232c292 commit 24d8dea

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
4+
vector<vector<int>>ans;
5+
if(!root)return ans;
6+
queue<TreeNode*>q;
7+
q.push(root);
8+
int i=0;
9+
while(!q.empty()){
10+
int sz=q.size();
11+
vector<int>v;
12+
while(sz--){
13+
TreeNode * f=q.front();
14+
v.push_back(q.front()->val);
15+
q.pop();
16+
if(f->left)q.push(f->left);
17+
if(f->right)q.push(f->right);
18+
19+
}
20+
if(i++%2)
21+
reverse(v.begin(),v.end());
22+
ans.push_back(v);
23+
24+
}
25+
return ans;
26+
27+
}
28+
};

0 commit comments

Comments
 (0)