Skip to content

Commit 230128c

Browse files
committedSep 12, 2013
Add a solution suggested by yinlinglin.
1 parent 41f3b0c commit 230128c

File tree

1 file changed

+67
-32
lines changed

1 file changed

+67
-32
lines changed
 

‎BinaryTreeZigzagLevelOrderTraversal.h

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
Author: Annie Kim, anniekim.pku@gmail.com
33
Date: May 16, 2013
4-
Update: Aug 17, 2013
4+
Update: Sep 12, 2013
55
Problem: Binary Tree Zigzag Level Order Traversal
66
Difficulty: Easy
77
Source: http://leetcode.com/onlinejudge#question_103
@@ -22,8 +22,9 @@
2222
[15,7]
2323
]
2424
25-
Solution: 1. BFS(queue) + reverse vector.
26-
2. Two stack.
25+
Solution: 1. Queue + reverse.
26+
2. Two stacks.
27+
3. Vector. Contributed by yinlinglin.
2728
*/
2829

2930
/**
@@ -39,80 +40,114 @@
3940
class Solution {
4041
public:
4142
vector<vector<int>> zigzagLevelOrder(TreeNode *root) {
42-
return zigzagLevelOrder_2(root);
43+
return zigzagLevelOrder_1(root);
4344
}
4445

46+
// solution 1: Queue + Reverse.
4547
vector<vector<int>> zigzagLevelOrder_1(TreeNode *root) {
4648
vector<vector<int>> res;
4749
if (!root) return res;
4850
queue<TreeNode *> q;
4951
q.push(root);
5052
q.push(NULL); // end indicator of one level
5153
bool left2right = true;
52-
int level = 0;
53-
while (!q.empty())
54+
vector<int> level;
55+
while (true)
5456
{
55-
TreeNode *front = q.front();
56-
q.pop();
57-
if (front)
57+
TreeNode *node = q.front(); q.pop();
58+
if (node)
5859
{
59-
if (res.size() == level)
60-
res.push_back(vector<int>());
61-
res[level].push_back(front->val);
62-
if (front->left)
63-
q.push(front->left);
64-
if (front->right)
65-
q.push(front->right);
60+
level.push_back(node->val);
61+
if (node->left) q.push(node->left);
62+
if (node->right) q.push(node->right);
6663
}
6764
else
6865
{
69-
if (!q.empty()) // CAUTIOUS! infinite loop
70-
q.push(NULL);
71-
if (!left2right)
72-
reverse(res[level].begin(), res[level].end());
66+
if (!left2right)
67+
reverse(level.begin(), level.end());
68+
res.push_back(level);
69+
level.clear();
70+
if (q.empty()) break;
71+
q.push(NULL);
7372
left2right = !left2right;
74-
level++;
7573
}
7674
}
7775
return res;
7876
}
7977

78+
// Solution 2: Two stacks.
8079
vector<vector<int>> zigzagLevelOrder_2(TreeNode *root) {
8180
vector<vector<int>> res;
8281
if (!root) return res;
8382
stack<TreeNode *> stk[2];
84-
bool left2right = false;
83+
bool left2right = true;
8584
int cur = 1, last = 0;
8685
stk[last].push(root);
87-
int level = 0;
86+
vector<int> level;
8887
while (!stk[last].empty())
8988
{
90-
TreeNode *node = stk[last].top();
89+
TreeNode *node = stk[last].top();
9190
stk[last].pop();
9291
if (node)
9392
{
94-
if (res.size() == level)
95-
res.push_back(vector<int>());
96-
res[level].push_back(node->val);
93+
level.push_back(node->val);
9794
if (left2right)
9895
{
99-
stk[cur].push(node->right);
10096
stk[cur].push(node->left);
97+
stk[cur].push(node->right);
10198
}
10299
else
103100
{
104-
stk[cur].push(node->left);
105101
stk[cur].push(node->right);
102+
stk[cur].push(node->left);
106103
}
107104
}
108105
if (stk[last].empty())
109106
{
110-
cur = !cur;
111-
last = !last;
107+
if (!level.empty())
108+
res.push_back(level);
109+
level.clear();
110+
swap(cur, last);
112111
left2right = !left2right;
113-
level++;
114112
}
115113
}
116114
return res;
117115
}
116+
117+
// Solution 3: Vector. Contributed by yinlinglin.
118+
// Compared to solution 1&2, this solution costs a little more space.
119+
// This solution uses only one single vector instead of two stacks in solution 2.
120+
vector<vector<int>> zigzagLevelOrder_3(TreeNode *root) {
121+
vector<vector<int>> result;
122+
if(!root) return result;
123+
vector<TreeNode*> v;
124+
v.push_back(root);
125+
bool left2right = true;
126+
int begin = 0, end = 0;
127+
while(begin <= end)
128+
{
129+
vector<int> row;
130+
for (int i = end; i >= begin; --i)
131+
{
132+
if (!v[i]) continue;
133+
row.push_back(v[i]->val);
134+
if(left2right)
135+
{
136+
v.push_back(v[i]->left);
137+
v.push_back(v[i]->right);
138+
}
139+
else
140+
{
141+
v.push_back(v[i]->right);
142+
v.push_back(v[i]->left);
143+
}
144+
}
145+
if (!row.empty())
146+
result.push_back(row);
147+
begin = end + 1;
148+
end = v.size() - 1;
149+
left2right = !left2right;
150+
}
151+
return result;
152+
}
118153
};

0 commit comments

Comments
 (0)
Please sign in to comment.