/
89_按层打印二叉树.cpp
148 lines (137 loc) · 3.62 KB
/
89_按层打印二叉树.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <queue>
#include <string>
#include <memory>
#include <algorithm>
using namespace std;
class Node {
public:
Node(int value) : value(value) { left = nullptr; right = nullptr; }
int value;
Node *left;
Node *right;
};
string getSpace(int num);
void printInOrder(Node *head, int height, string to, int len);
void printTree(Node *head);
void printVector(vector<vector<int>>& arr);
/*
按层打印二叉树
*/
vector<vector<int>> func_1(Node* head) {
vector<vector<int> > ret;
if (nullptr == head) {
return ret;
}
queue<Node*> q;
q.push(head);
while (!q.empty()) {
int size = q.size();
vector<int> temp;
while (size--) {
Node* cur = q.front();
q.pop();
temp.push_back(cur->value);
if (cur->left != nullptr) {
q.push(cur->left);
}
if (cur->right != nullptr) {
q.push(cur->right);
}
}
ret.push_back(temp);
}
return ret;
}
vector<int> func_2(Node* root) {
vector<int> ret;
if (nullptr == root)
return ret;
queue<Node *> q;
q.push(root);
while (!q.empty()) {
root = q.front();
q.pop();
ret.push_back(root->value);
if (root->left != nullptr)
q.push(root->left);
if (root->right != nullptr)
q.push(root->right);
}
return ret;
}
int main() {
Node *head_1 = &Node(1);
head_1->left = &Node(2);
head_1->right = &Node(3);
head_1->left->left = &Node(4);
head_1->left->right = &Node(5);
head_1->right->left = &Node(6);
head_1->right->right = &Node(7);
Node *head_2 = &Node(5);
head_2->left = &Node(3);
head_2->right = &Node(8);
head_2->left->left = &Node(2);
head_2->left->right = &Node(4);
head_2->left->left->left = &Node(1);
head_2->right->left = &Node(7);
head_2->right->left->left = &Node(6);
head_2->right->right = &Node(10);
head_2->right->right->left = &Node(9);
head_2->right->right->right = &Node(11);
printTree(head_1);
cout << "=============================================" << endl;
printTree(head_2);
cout << "=============================================" << endl;
vector<vector<int>> ret;
vector<int> res;
ret = func_1(head_1);
printVector(ret);
res = func_2(head_1);
for (auto i : res) {
cout << i << " ";
}
cout << endl << "=============================================" << endl;
ret = func_1(head_2);
printVector(ret);
res = func_2(head_2);
for (auto i : res) {
cout << i << " ";
}
cout << endl;
return 0;
}
void printTree(Node *head) {
cout << "Binary Tree:" << endl;
printInOrder(head, 0, "H", 17);
cout << endl;
}
void printInOrder(Node *head, int height, string to, int len) {
if (head == nullptr) {
return;
}
printInOrder(head->right, height + 1, "v", len);
string val = to + to_string(head->value) + to;
int lenM = val.length();
int lenL = (len - lenM) / 2;
int lenR = len - lenM - lenL;
val = getSpace(lenL) + val + getSpace(lenR);
cout << (getSpace(height * len) + val).c_str() << endl;
printInOrder(head->left, height + 1, "^", len);
}
string getSpace(int num) {
string space = " ";
string buf;
for (int i = 0; i < num; i++) {
buf.append(space.c_str());
}
return buf;
}
void printVector(vector<vector<int>>& arr) {
for (size_t i = 0; i < arr.size(); i++) {
for (size_t j = 0; j < arr[i].size(); j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}