BJFUOJ
#include<bits/stdc++.h>
using namespace std;
bool flag = true;
typedef struct BiTnode {
char data;
struct BiTnode *lchild, *rchild;
} BiTnode, *BiTree;
void CreatTree(BiTree &l, char c) {
if (l == NULL) {
l = new BiTnode;
l->data = c;
l->lchild = NULL;
l->rchild = NULL;
} else if (l->data > c) {
CreatTree(l->lchild, c);
} else {
CreatTree(l->rchild, c);
}
}
int Hight(BiTree &l) {
if (l == NULL) return 0;
else if (l->lchild == NULL && l->rchild == NULL) {
return 1;
} else {
return max(Hight(l->lchild), Hight(l->rchild)) + 1;
}
}
bool Judge(BiTree &l) {
if (l == NULL) return true;
queue <BiTree> q;
q.push(l);
bool leaf = false;
while (!q.empty()) {
BiTree p = q.front();
q.pop();
if ((p->rchild != NULL && p->lchild == NULL) || (leaf && (l->lchild != NULL || l->rchild != NULL))){
return false;
}
if(l->lchild != NULL){
q.push(l->lchild);
}
if(l->rchild){
q.push(l->rchild);
}
if((p->lchild != NULL && p->rchild == NULL)||(p->rchild == NULL && p->lchild == NULL)){
leaf = true;
}
}
return true;
}
void OutPut(BiTree &l) {
if (l) {
OutPut(l->lchild);
cout << l->data << " ";
OutPut(l->rchild);
}
}
void BracketsOut(BiTree &l){
cout<<"(";
if(l){
cout<<l->data;
if(l->lchild){
BracketsOut(l->lchild);
}
if(l->rchild){
cout<<",";
BracketsOut(l->rchild);
}
}
cout<<")";
}
void CreatLinklist(BiTree &l,BiTree &head,BiTree &r){
if(l){
CreatLinklist(l->lchild,head,r);
CreatLinklist(l->rchild,head,r);
if(l->lchild == NULL && l->rchild == NULL){
if(flag){
head = new BiTnode;
r = l;
head->lchild = r;
flag = false;
}else{
r->lchild = l;
l->rchild = r;
r = l;
}
}
r->lchild = NULL;
}
}
void OutLeaf(BiTree &head){
BiTree r = head->lchild;
while(r){
cout<<r->data<<" ";
r = r->lchild;
}
}
int main() {
char s[100];
cin >> s;
BiTree l = NULL;
for (int i = 0; i < strlen(s); i++) {
CreatTree(l, s[i]);
}
OutPut(l);
cout << endl;
cout << Hight(l) << endl;
if (Judge(l)) {
cout << "是" << endl;
} else {
cout << "否" << endl;
}
BiTree head,r;
CreatLinklist(l,head,r);
OutLeaf(head);
return 0;
}