Skip to content

Commit 6a73a2a

Browse files
committed
feat:add Solution.cpp for 0103. Binary Tree Zigzag Level Order Traversal
1 parent 0d436da commit 6a73a2a

File tree

1 file changed

+23
-0
lines changed
  • solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)