Code & Func
2018-02-11

第97天。

今天的题目是Binary Tree Zigzag Level Order Traversal:

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,null,null,15,7],

1
3
2
/ \
3
9 20
4
/ \
5
15 7

return its zigzag level order traversal as:

1
[
2
[3],
3
[20,9],
4
[15,7]
5
]

首先想到的是用层次遍历的方式来实现。

简单的层次遍历:

1
void levelTra(TreeNode *root) {
2
if (root == nullptr) return ;
3
queue<TreeNode *> q;
4
q.push(root);
5
while(!q.empty()) {
6
root = q.front(); q.pop();
7
cout << root->val;
8
if (root->left) q.push(root->left);
9
if (root->right) q.push(root->right);
10
}
11
}

但是上面的方法是没法区分层数的,我们通过nullptr来表示换行:

1
void levelTra(TreeNode *root) {
2
if (root == nullptr) return ;
3
queue<TreeNode *> q;
4
q.push(root);
5
q.push(nullptr);
6
while(true) {
7
root = q.top(); q.pop();
8
9
if (root == nullptr) {
10
cout << "new level" << endl;
11
if (q.empty()) break;
12
q.push(nullptr);
13
}
14
15
cout << root->val << endl;
4 collapsed lines
16
if (root->left) q.push(root->left);
17
if (root->right) q.push(root->right);
18
}
19
}
1
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
2
vector<vector<int> > ret;
3
vector<int> tmp;
4
if (root == nullptr) return ret;
5
//level tra
6
7
bool flag = true; //判断遍历方向
8
9
queue<TreeNode *> q;
10
q.push(root);
11
q.push(nullptr);
12
13
while(true) {
14
root = q.front(); q.pop();
15
if (root == nullptr) {
20 collapsed lines
16
17
if (flag) ret.push_back(tmp);
18
else ret.push_back(vector<int>(tmp.rbegin(), tmp.rend()));
19
20
tmp.clear();
21
flag = !flag;
22
23
if (q.empty()) break;
24
q.push(nullptr);
25
continue;
26
}
27
28
29
tmp.push_back(root->val);
30
if (root->left) q.push(root->left);
31
if (root->right) q.push(root->right);
32
}
33
34
return ret;
35
}

然后是dicuss中的方法,简单的来说就是通过深度优先遍历来生成获取层次遍历的每层的数组(好像之前写过?),然后就会比前面用queue的方法快。

1
void travel(TreeNode *root, vector<vector<int> > &ret, int level) {
2
if (root == nullptr) return ;
3
if (level >= ret.size()) ret.push_back(vector<int>());
4
5
ret[level].push_back(root->val);
6
7
travel(root->left, ret, level + 1);
8
travel(root->right, ret, level + 1);
9
}
10
11
vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
12
vector<vector<int>> ret;
13
travel(root, ret, 0);
14
15
for(int i = 0;i < ret.size();i++) {
5 collapsed lines
16
if (i % 2) reverse(ret[i].begin(), ret[i].end());
17
}
18
19
return ret;
20
}

Update as 2020-03-28

最近在总结 Stack Tag 的算法,然后发现这道题可以用双栈来解,和前面队列的做法有点类似,某种意义上也是在模拟层次遍历,但是因为栈后进先出的特性,所以很直接的实现了逆序的操作,不需要额外做reverse

1
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
2
vector<vector<int> > res;
3
int level = 0;
4
stack<TreeNode*> st_even, st_odd;
5
if (root) st_even.push(root);
6
while(!st_even.empty() || !st_odd.empty()) {
7
stack<TreeNode*> &st1 = level % 2 == 0 ? st_even : st_odd;
8
stack<TreeNode*> &st2 = level % 2 == 0 ? st_odd : st_even;
9
vector<int> temp;
10
for(int i = 0, size = st1.size(); i < size; i++) {
11
root = st1.top(); st1.pop();
12
temp.push_back(root->val);
13
14
TreeNode *left = root->left, *right = root->right;
15
if (level % 2) swap(left, right);
10 collapsed lines
16
17
if (left) st2.push(left);
18
if (right) st2.push(right);
19
}
20
//cout << st1.size() << " " << st2.size() << endl;
21
res.push_back(temp);
22
level++;
23
}
24
return res;
25
}
上一条动态