/
c101. 00122 - Trees on the level.cpp
149 lines (134 loc) · 4.81 KB
/
c101. 00122 - Trees on the level.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
149
//2023/03/20 kerong
#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
//=======<binary tree結構>=========
struct TreeNode {
int val; //節點的數值
TreeNode *left; //節點的左分支
TreeNode *right; //節點的右分之
TreeNode() : val(-1), left(nullptr), right(nullptr) {} //完全沒有資訊的建構子
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} //只有數值的建構子
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} //完整資訊的建構子
};
int string_to_int(string str){
int power = 1;
int ans = 0;
for(int x=str.size()-1;x>=1;x--){
ans += (str[x]-'0') * power;
power *= 10;
}
return ans;
}
//========<每層打印>==============
void level_order(TreeNode *root, const int max_node_cnt){
int root_cnt = 1;
queue<TreeNode *> q;
q.push(root);
while(!q.empty()){
if(root_cnt++ > max_node_cnt){
cout << q.front()->val;
}
else{
cout << q.front()->val << " ";
}
if(q.front()->left){
q.push(q.front()->left);
}
if(q.front()->right){
q.push(q.front()->right);
}
q.pop();
}
}
int main(int argc, char **argv){
string buffer; //把切割的緩衝
string val_data; //切割後數值
while(true){
TreeNode *head = new TreeNode();
int root_count = -1; //節點填滿的數量
bool is_equal = false; //重複節點問題
bool end_test = true;
int node_cnt = 0;
while(cin>>buffer){
end_test = false;
//=======<終止條件>===========//
if(buffer.size()==2){
break;
}
//======<根據節點資料,做處理>=======//
buffer[0] = ' '; //把(用掉
buffer[buffer.size()-1] = ' '; //把)用掉
int root_val = 0; //節點數值
string root_position; //節點位置資訊
val_data = buffer.substr(0, buffer.find(","));
root_val = string_to_int(val_data); //獲取數值
root_position = buffer.substr(buffer.find(",")+1, buffer.size()-1); //獲取位置
TreeNode *node = head; //獲取頭資料
if(root_position.size() == 1){ //最根部處理
if(node->val == -1){
node->val = root_val;
root_count += 1; //找到根部填充
}
else{
is_equal = true; //找到重複根部
}
}
else{
for(int x=0;x<root_position.size()-1;x++){
if(root_position[x]=='L'){ //往左探索
if(node->left==NULL){
TreeNode *temp = new TreeNode();
node->left = temp;
node = node->left;
root_count -= 1; //空的節點
}
else{
node = node->left;
}
}
else if(root_position[x]=='R'){ //往右探索
if(node->right==NULL){
TreeNode *temp = new TreeNode();
node->right = temp;
node = node->right;
root_count -= 1; //空的節點
}
else{
node = node->right;
}
}
}
//======<放到沒有填充的位置>=======//
if(node->val == -1){
node->val = root_val;
root_count += 1;
node_cnt += 1;
}
else{
is_equal = true; //重複的
}
}
}
if(end_test){
break;
}
//======<存在重複節點>======//
if(is_equal || root_count!=0){
cout << "not complete" << endl;
}
else{
//postorder(head);
level_order(head, node_cnt);
cout << endl;
}
}
return 0;
}
/*
input:
(11,LL) (7,LLL) (8,R) (5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()
*/