forked from Anchor89/LeetCodeAnchor89
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTreeZigzagLevelOrderTraversal.cpp
47 lines (46 loc) · 1.32 KB
/
BinaryTreeZigzagLevelOrderTraversal.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
47
#include "LeetCode.h"
class Solution {
public:
// Not good. Should do normal level order traversal and output in different order
vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
vector<vector<int> > res;
vector<int> row;
if (!root) return res;
vector<TreeNode*> lvl[2];
int cur = 0;
lvl[cur].push_back(root);
res.push_back(vector<int>());
res.back().push_back(root->val);
while(!lvl[cur].empty()) {
lvl[1-cur].clear();
row.clear();
for_each(lvl[cur].rbegin(), lvl[cur].rend(), [&](TreeNode* n) {
if (n->right) {
lvl[1-cur].push_back(n->right);
row.push_back(n->right->val);
}
if (n->left) {
lvl[1-cur].push_back(n->left);
row.push_back(n->left->val);
}
});
if (!row.empty()) res.push_back(row);
cur = 1-cur;
lvl[1-cur].clear();
row.clear();
for_each(lvl[cur].rbegin(), lvl[cur].rend(), [&](TreeNode* n) {
if (n->left) {
lvl[1-cur].push_back(n->left);
row.push_back(n->left->val);
}
if (n->right) {
lvl[1-cur].push_back(n->right);
row.push_back(n->right->val);
}
});
if (!row.empty()) res.push_back(row);
cur = 1-cur;
}
return res;
}
};