Follows 122.c (Total 132 lines):

/* @JUDGE_ID:4461XX 122 C */
/* A */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct tree{
	int num ;
	struct tree *left ;
	struct tree *right ;
} ;
struct queue{
	int data ;
	struct tree *where ;
	struct queue *next ;
} ;
struct tree *root ;
struct queue *root_q ;
char arr[300] ;
int make_tree( void )
{
	int n , i ;
	char way[300] ;
	struct tree *p , *wait ;
	sscanf( arr , "(%d,%s", &n , way ) ;
	way[strlen( way )-1] = '\0' ;

	if( *way == '0' ) way[0] = '\0' ;

	if( !strlen( way ) )
		if( !root->num ) root->num = n ;
		else return 0 ;
	else{
		for( wait=root , i=0 ; way[i] ; i++ ){
		p = ( struct tree * )malloc( sizeof( struct tree ) ) ;
		p->num = 0 ;
		p->right = p->left = NULL ;
		if( way[i] == 'R' )
			if( wait->right ){
				wait = wait->right ;
				free( p ) ;
			}
			else{
				wait->right = p ;
				wait = p ;
			}
		else
			if( wait->left ){
				wait = wait->left ;
				free( p ) ;
			}
			else{
				wait->left = p ;
				wait = p ;
			}
		}
		if( !wait->num ) wait->num = n ;
		else return 0 ;
	}
	return 1 ;
}
void make_queue( void )
{
	int firstOutput;
	struct queue *head , *tail , *p , *k ;
	head = ( struct queue * )malloc( sizeof( struct queue ) ) ;
	head->where = root ;
	head->next = NULL ;
	head->data = head->where->num ;
	root_q = tail = head ;
	do{
		if( !tail->data ){
			printf( "not complete\n" ) ;
			return ;
		}
		p = ( struct queue * )malloc( sizeof( struct queue ) ) ;
		p->next = NULL ;
		if( tail->where->left ){
			p->data = tail->where->left->num ;
			p->where = tail->where->left ;
			head->next = p ;
			head = p ;
		}
		else free( p ) ;
		p = ( struct queue * )malloc( sizeof( struct queue ) ) ;
		p->next = NULL ;
		if( tail->where->right ){
			p->data = tail->where->right->num ;
			p->where = tail->where->right ;
			head->next = p ;
			head = p ;
		}
		else free( p ) ;
		free( tail->where ) ;
		tail->where = NULL ;
		tail = tail->next ;
	}while( tail ) ;
	for( p=root_q, firstOutput=1 ; p ; k=p , p=p->next , free( k ) ){
		if( firstOutput ){
			printf( "%d" , p->data ) ;
			firstOutput = 0;
		}
		else
			printf( " %d" , p->data ) ;
	}
	putchar( '\n' ) ;
}
void main( void )
{
	int yes ;
/*	freopen( "C:\\windows\\desktop\\122.in" , "r" , stdin ) ;
	freopen( "C:\\windows\\desktop\\122.out" , "w" , stdout ) ;*/
	while( scanf( "%s" , arr ) == 1 ){
		yes = 1 ;
		if( !strcmp( "" , arr ) ) continue ;
		if( arr[1] == ')' ) yes = 0 ;
		else{
			root = ( struct tree * )malloc( sizeof( struct tree ) ) ;
			root->num = 0 ;
			root->right = root->left = NULL ;
			if( !make_tree() ) yes = 0 ;
			while( scanf( "%s" , arr ) == 1 ){
				if( !strcmp( "" , arr ) ) continue ;
				if( arr[1] != ')' ){
					if( !make_tree() ) yes = 0 ;
				}
				else break ;
			}
			if( yes ) make_queue() ;
			else printf( "not complete\n" ) ;
		}
	}
}

Back to statistics
Ya-Lin Huang (C)
huangyl@gmail.com