-
Notifications
You must be signed in to change notification settings - Fork 1
/
103.cpp
46 lines (41 loc) · 1.28 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
// 103. Binary Tree Zigzag Level Order Traversal - https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal
#include "bits/stdc++.h"
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
if (root == nullptr) { return {}; }
vector<vector<int> > result;
queue<pair<TreeNode*, int>> q;
q.push({root, 0});
int level;
while (!q.empty()) {
auto p = q.front();
q.pop();
level = p.second;
if (result.size() == level) { result.push_back({}); }
result[level].emplace_back(p.first->val);
auto vec = {p.first->left, p.first->right};
for (auto node : vec) {
if (node == nullptr) { continue; }
q.push({node, p.second + 1});
}
}
for (int idx = 0; idx < (int)result.size(); ++idx) {
if (idx % 2 == 0) { continue; }
reverse(result[idx].begin(), result[idx].end());
}
return result;
}
};
int main() {
ios::sync_with_stdio(false);
return 0;
}