We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ff46abe commit 6f59743Copy full SHA for 6f59743
zigzagLevelOrder.cpp
@@ -0,0 +1,28 @@
1
+class Solution {
2
+public:
3
+ vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
4
+ if(!root) return vector<vector<int>>();
5
+ queue<TreeNode*> q;
6
+ vector<vector<int>> res;
7
+ q.push(root);
8
+ bool ltr = true;
9
+ int i;
10
+ while(!q.empty()) {
11
+ int s = q.size();
12
+ vector<int> curLevel(s);
13
+ for(i=0; i < s; i++) {
14
+ TreeNode* cur = q.front();
15
+ q.pop();
16
+ int index = ltr ? i : s-1-i;
17
+ curLevel[index] = cur->val;
18
+ if(cur->left) q.push(cur->left);
19
+ if(cur->right) q.push(cur->right);
20
+
21
+ }
22
+ if(!ltr) ltr = true;
23
+ else ltr = false;
24
+ res.push_back(curLevel);
25
26
+ return res;
27
28
+};
0 commit comments