刚学二叉树,wa 了qaq
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#define line cout << endl
using namespace std;
struct node {
string val;
node *left, *right;
node () {
left = right = NULL;
val = "?";
}
} *root;
string s;
bool check(node *now) {
if (now->val == "?" ||
now->left != NULL && !check(now->left) ||
now->right != NULL && !check(now->right))
return false;
return true;
}
void bfs() {
queue<node*> q;
cout << root->val;
q.push(root);
while (!q.empty()) {
node *now = q.front();
q.pop();
if (now->left != NULL) {
cout << " " << now->left->val;
q.push(now->left);
}
if (now->right != NULL) {
cout << " " << now->right->val;
q.push(now->right);
}
}
return;
}
int main() {
while (cin >> s) {
root = new node;
bool valid = true;
while (s != "()") {
node *now = root;
for (int i = s.find(",") + 1; i < s.size() - 1; i++)
if (s[i] == 'L') {
if (now->left == NULL)
now->left = new node;
now = now->left;
}
else {
if (now->left == NULL)
now->left = new node;
now = now->left;
}
if (now->val == "?") now->val = s.substr(1, s.find(",") - 1);
else valid = false;
cin >> s;
}
if (valid && check(root)) bfs();
else cout << "not complete";
puts("");
}
return 0;
}