zoj 1700 二叉搜索树

题意:依次删除一棵树的叶节点,直到整棵树删完,输入就是删除的顺序,输出这棵树的先序遍历

简单题

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

char str[1000][1000];
char cstr[1000010] ;
int cnt,counter;

struct tree{
    tree *l,*r;
    char key ;
}node[1010];
tree *head ;
void Insert(tree * &head , int now)
{
    //cout<<now;
    if(head == NULL){
        //cout<<cstr[now]<<endl
        node[counter].key=cstr[now] ;
        head = &node[counter] ;
        counter++;
        return ;
    }
    else{

        if(cstr[now]<head->key) Insert(head->l,now) ;
        else Insert(head->r,now) ;
    }

}
void Print(tree *head)
{
    if(head==NULL){
        return ;
    }
    cout<<head->key;
    Print(head->l);
    Print(head->r);
}
void solve()
{

    head=NULL;
    int len1 = 0 ;
    for(int i=cnt-1;i>=0;i--){
        int len = strlen(str[i]) ;
        for(int j=len-1; j>=0 ;j--){
            cstr[len1++] = str[i][j] ;
        }
    }
    for(int i=0 ; i<len1 ; i++){
        Insert(head,i) ;
    }
    Print(head);
    cout<<endl;
}

void Initial()
{
    counter=0;
    memset(str,'\0', sizeof str);
    memset(cstr,'\0',sizeof cstr);
    memset(node,NULL,sizeof(node));
}

int main()
{
    Initial();
    cnt=0;
    while(gets(str[cnt])){
        if(str[cnt][0]=='$'){
            //cout<<str[cnt]<<endl;
            solve() ;
            return 0;
        }
        else if(!strcmp(str[cnt],"*")){
            solve();
            cnt=0;
            Initial();
        }
        else{
            cnt++;
        }
    }
    return 0;
}

 

posted @ 2015-08-06 10:46  Scale_the_heights  阅读(147)  评论(0)    收藏  举报