先建树 然后前序遍历
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<map>
using namespace std;
struct node {
node (char _v): v(_v) {
left = NULL;
right = NULL;
};
char v;
node *left;
node *right;
};
node *root;
void insert(node *curnode, node *nd) {
if (nd->v < curnode->v) {
if (curnode->left == NULL) {
curnode->left = nd;
} else {
insert(curnode->left, nd);
}
} else {
if (curnode->right == NULL) {
curnode->right = nd;
} else {
insert(curnode->right, nd);
}
}
}
void pre(node *nd) {
cout << nd->v;
if (nd->left != NULL)pre(nd->left);
if (nd->right != NULL)pre(nd->right);
}
void solve(vector<string> v, string str) {
int sz, i, j, len;
sz = v.size();
root = new node(v[sz - 1][0]);
for (i = sz - 2; i >= 0; i--) {
str = v[i];
len = str.length();
for (j = 0; j < len; j++) {
node *nd = new node(str[j]);
insert(root, nd);
}
}
pre(root);
printf("\n");
}
int main() {
vector<string> v;
string str;
while (cin >> str) {
if (str == "$") {
solve(v, str);
break;
}
if (str != "*") {
v.push_back(str);
} else {
solve(v, str);
v.clear();
}
}
}