We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0d436da commit 6a73a2aCopy full SHA for 6a73a2a
solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/Solution.cpp
@@ -0,0 +1,23 @@
1
+class Solution {
2
+public:
3
+ vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
4
+ if (!root) return {};
5
+ vector<vector<int>> res;
6
+ queue<TreeNode*> q{{root}};
7
+ bool leftToRight = true;
8
+ while (!q.empty()) {
9
+ int size = q.size();
10
+ vector<int> oneLevel(size);
11
+ for (int i = 0; i < size; ++i) {
12
+ TreeNode* t = q.front(); q.pop();
13
+ int idx = leftToRight ? i : (size - 1 - i);
14
+ oneLevel[idx] = t->val;
15
+ if (t->left) q.push(t->left);
16
+ if (t->right) q.push(t->right);
17
+ }
18
+ leftToRight = !leftToRight;
19
+ res.push_back(oneLevel);
20
21
+ return res;
22
23
+};
0 commit comments