C-二分木のプレオーダートラバーサルを見つける

説明

二分木の中次走査と後次走査が与えられた場合、二分木の1次探索を見つけます
入力

入力データのセットは複数あります。最初の行は整数t(t <1000)です。これは、テストデータのセットがt個あることを意味します。各グループには、長さが50未満の2つの文字列が含まれます。最初の文字列は二分木の中間の走査シーケンスを表し、2番目の文字列は二分木の後次の走査シーケンスを表します。
出力

二分木の
サンプルのプレオーダートラバーサルシーケンスを出力します

入力

2
dbgeafc
dgebfca
lnixu
linuxの
出力

abdegcf
xnliu

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char a[100],b[100];
struct node
{
    
    
    char data;
    struct node*lchild,*rchild;
};
struct node *creat(char *a,char*b,int len)
{
    
    
    int i;
    if(len==0)
        return NULL;
    struct node *root;
    root=(struct node*)malloc(sizeof(struct node));
    root->data=b[len-1];
    for(i=0;i<len;i++)
    {
    
    
        if(b[len-1]==a[i])
            break;
    }
    root->lchild=creat(a,b,i);
    root->rchild=creat(a+i+1,b+i,len-i-1);
    return root;
}
void front(struct node*root)
{
    
    
    if(root)
    {
    
    
        printf("%c",root->data);
        front(root->lchild);
        front(root->rchild);

    }
}
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        scanf("%s%s",a,b);
    struct node*root;
    root=(struct node*)malloc(sizeof(struct node));
    int len=strlen(a);
    root=creat(a,b,len);
    front(root);
    printf("\n");
    }

    return 0;
}

おすすめ

転載: blog.csdn.net/a675891/article/details/103961462