-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Tree Zigzag Level Order Traversal.cpp
80 lines (76 loc) · 2.28 KB
/
Binary Tree Zigzag Level Order Traversal.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Given a binary tree, return the zigzag level order traversal of
* its nodes' values. (ie, from left to right, then right to left
* for the next level and alternate between).
*
* For example:
* Given binary tree {3,9,20,#,#,15,7},
* 3
* / \
* 9 20
* / \
* 15 7
* return its zigzag level order traversal as:
* [
* [3],
* [20,9],
* [15,7]
* ]
*/
/**
* Definition for binary tree
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<TreeNode *> buf_l, buf_r;
vector<vector<int> > result;
bool l2r = true; // node rendering direction (true: left to right)
if (root == nullptr) {
return result;
}
buf_l.push_back(root);
while (!buf_l.empty() || !buf_r.empty()) {
vector<int> v;
if (l2r) {
// iterate from left to right
while (!buf_l.empty()) {
TreeNode *cur = buf_l.back();
buf_l.pop_back();
v.push_back(cur->val);
if (cur->left != nullptr) {
buf_r.push_back(cur->left);
}
if (cur->right != nullptr) {
buf_r.push_back(cur->right);
}
}
l2r = false;
} else {
// iterate from right to left
while (!buf_r.empty()) {
TreeNode *cur = buf_r.back();
buf_r.pop_back();
v.push_back(cur->val);
if (cur->right != nullptr) {
buf_l.push_back(cur->right);
}
if (cur->left != nullptr) {
buf_l.push_back(cur->left);
}
}
l2r = true;
}
result.push_back(v);
}
return result;
}
};