http://211.67.33.62/JudgeOnline/problem.php?id=1014,排序二叉树

来源:互联网 发布:淘宝升级企业店铺入口 编辑:程序博客网 时间:2024/05/22 00:12

/********已知先、中序遍历,输出后序遍历**********/

#include <iostream>
#include <cstdio>
#include <cstring>
#define N 101
using namespace std;

void print(char *s1, char *s2, int n)//这是主要函数,s1指向先序遍历,s2则中序,n为长度
{
   char *st = strchr(s2,s1[0]);
/********原型:extern char *strchr(const char *s,char c);
const char *strchr(const char* _Str,int _Val)
char *strchr(char* _Str,int _Ch)
头文件:#include <string.h>
功能:查找字符串s中首次出现字符c的位置
说明:返回首次出现c的位置的指针,
返回的地址是字符串在内存中
随机分配的地址再加上你所搜索的字符在字符串位置,
如果s中不存在c则返回NULL。********/
   if(n==0)
     return;
   print(s1+1,s2,st-s2);
   print(s1+1+(st-s2),st+1,n-1-(st-s2));
   printf("%c",s1[0]);
}

int main()
{
    int n;
    char s1[N],s2[N];
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",s1);
        scanf("%s",s2);
        int num=strlen(s2);
        print(s1,s2,num);
        printf("\n");
    }
    return 0;
}

原创粉丝点击