-
Notifications
You must be signed in to change notification settings - Fork 0
/
按之字形顺序打印二叉树.cpp
55 lines (48 loc) · 1.51 KB
/
按之字形顺序打印二叉树.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
//请实现一个函数按照之字形打印二叉树,
//即第一行按照从左到右的顺序打印,
//第二层按照从右至左的顺序打印,
//第三行按照从左到右的顺序打印,其他行以此类推。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int>> result(0);
if (!pRoot)
return result;
int flag = 0;
vector<TreeNode*> cur(0);
cur.push_back(pRoot);
while (cur.size() > 0) {
vector<int> temp(0);
if (flag == 0) {
for (int i = 0; i < cur.size(); ++i)
temp.push_back(cur[i]->val);
}
if (flag == 1) {
for (int i = cur.size()-1; i >= 0; --i)
temp.push_back(cur[i]->val);
}
result.push_back(temp);
flag = (++flag) % 2;
vector<TreeNode*> tempCur(0);
for (int i = 0; i < cur.size(); ++i) {
if (cur[i]->left)
tempCur.push_back(cur[i]->left);
if (cur[i]->right)
tempCur.push_back(cur[i]->right);
}
swap(cur, tempCur);
vector<int>(0).swap(temp);
}
return result;
}
};