洛谷_P1305 新二叉树_data_x_y_建树

洛谷_P1305 新二叉树_data_x_y_建树

//
#include<bits/stdc++.h>
using namespace std;
#define T char

struct node
{
    T data;
    node *x,*y;
    node( T in=0,node *xx=NULL,node *yy=NULL ):data(in),x(xx),y(yy) {}
}rr;

node* new_node( T data )
{
    if( data=='*' ) return NULL;
    return new node( data );
}

node* find_node( T data,node *root=&rr )            // &rr
{   
    if( root->data==data ) return root;
    node *ans=NULL;
    if( root->x ) ans=find_node( data,root->x );    // find_xx
    if( ans ) return ans;                           // ans!=NULL
    if( root->y ) ans=find_node( data,root->y );    // find_yy
    return ans;
}

string s;
void AA( node *root )
{
    if( root==NULL ) return ;
    s+=root->data;
    AA( root->x );
    AA( root->y );
}

int main()
{
    int n;
    T data,x,y;                             // T
    
    while( cin>>n )
    {
        cin>>data>>x>>y;                    // rr.
        rr.data=data; rr.x=new_node( x ); rr.y=new_node( y );

        while( --n )
        {
            cin>>data>>x>>y;
            node *tt=find_node( data );
            tt->x=new_node( x );            // tt->
            tt->y=new_node( y );
        }
        s.clear(); AA( &rr );               // &rr
        cout<<s<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_63173957/article/details/125030467