蓝桥杯练习题—— 求先序排列

来源:互联网 发布:淘宝助理5.4版本下载 编辑:程序博客网 时间:2024/05/16 02:48
/*************************************    Title: 蓝桥杯练习题—— 求先序排列************************************    Date:2014/03/21************************************    author:刘旭*************************************/#include<iostream>using namespace std;string a, b;typedef struct binary_tree{    char data;    binary_tree *left, *right;} bt;bt* creat(){    bt *n = new bt;    if(!n)        return 0;    n->left = n->right = NULL;    return n;}int visit(bt *root){    cout<<root->data;    if(root->left)        visit(root->left);    if(root->right)        visit(root->right);    delete(root);    root = NULL;    return 0;}bt* build(int posa,int posb,int len){    bt *root = NULL;    if(len > 0)    {        root = creat();        root->data = b[posb];        int pos = a.find(root->data);        int lenl =pos - posa;        int lenr =len -1 - lenl;        root->left = build(posa, posb-lenr-1, lenl);        root->right = build(pos +1, posb-1, lenr);    }    return root;}int main(){    while(cin>>a>>b)    {        bt *root = build(0, b.length()-1, b.length());        visit(root);        cout<<endl;    }    return 0;}

0 0