https://vjudge.net/problem/UVA-122
#include<cstdio>
#include<math.h>
#include<cstring>
#include<climits>
#include<string>
#include<queue>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<climits>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<list>
#include<algorithm>
using namespace std;
#define rep(i,j,k)for(i=j;i<k;i++)
#define per(i,j,k)for(i=j;i>k;i--)
#define MS(x,y)memset(x,y,sizeof(x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
#define abs(x) (x>0?x:-x)
const int INF=0x7ffffff;
const ll MAX=1e18;
typedef struct tnode
{
int V;
tnode* L;
tnode* R;
}tree;
tree node[300];
tree* root;
int tree_size,complete;
int test(tree *root)
{
if(!root->V)return 0;
int ans=0;
if(!root->L||test(root->L))
ans++;
if(!root->R||test(root->R))
ans++;
return ans==2;
}
void madtree(tree *root,char *str,int v)
{
if(*str=='R'){
if(!root->R)
root->R=&node[++tree_size];
madtree(root->R,str+1,v);
}
else if(*str=='L'){
if(!root->L)
root->L=&node[++tree_size];
madtree(root->L,str+1,v);
}
else {
if(root->V)
complete=0;
root->V=v;
}
}
tree *q[300];
void output(tree *root)
{
int mov=0,sav=0;
q[sav++]=root;
printf("%d",root->V);
while(mov<sav){
tree *now=q[mov++];
if(now->L){
printf(" %d",now->L->V);
q[sav++]=now->L;
}
if(now->R){
printf(" %d",now->R->V);
q[sav++]=now->R;
}
}
printf("\n");
}
int main()
{
char buf[256],leaf[256];
int value;
complete=1;
root=&node[tree_size=0];
while(~scanf("%s",buf)){
if(!strcmp(buf,"()")){
if(!complete||!test(root))
printf("not complete\n");
else output(root);
memset(node,0,sizeof(node));
root=&node[tree_size=0];
complete=1;
}
else {
sscanf(buf,"(%d,%s",&value,leaf);
madtree(root,leaf,value);
}
}
return 0;
}