这我课后作业
2025-07-19 19:18:30
发布于:上海
1阅读
0回复
0点赞
嗨嗨嗨
#include <iostream>
#include <string>
using namespace std;
struct FBI_Node {
char type;
FBI_Node* left;
FBI_Node* right;
FBI_Node(char t) : type(t), left(nullptr), right(nullptr) {}
};
char determine_type(const string& s) {
bool has_zero = false;
bool has_one = false;
for (char c : s) {
if (c == '0') has_zero = true;
else has_one = true;
if (has_zero && has_one) break;
}
if (has_zero && has_one) return 'F';
else if (has_zero) return 'B';
else return 'I';
}
FBI_Node* build_tree(const string& s) {
char type = determine_type(s);
FBI_Node* node = new FBI_Node(type);
if (s.length() > 1) {
int mid = s.length() / 2;
string left_str = s.substr(0, mid);
string right_str = s.substr(mid);
node->left = build_tree(left_str);
node->right = build_tree(right_str);
}
return node;
}
void postorder(FBI_Node* node, string& result) {
if (node == nullptr) return;
postorder(node->left, result);
postorder(node->right, result);
result += node->type;
}
int main() {
int N;
string S;
cin >> N >> S;
FBI_Node* root = build_tree(S);
string result;
postorder(root, result);
cout << result << endl;
return 0;
}
这里空空如也
有帮助,赞一个