poj1741,poj 2255 Tree Recovery

 2023-09-20 阅读 26 评论 0

摘要:#include <iostream> //给出先序和中序遍历序列,输出后序遍历序列#include <string.h>using namespace std;const int MAXLEN=100;void build(int len,char* s1,char* s2,char* s) //s1,s2和s都是在不断变化,所以s1[0]...也随之不同{if(len<=0)return
#include <iostream>        //给出先序和中序遍历序列,输出后序遍历序列
#include <string.h>
using namespace std;
const int MAXLEN=100;
void build(int len,char* s1,char* s2,char* s) //s1,s2和s都是在不断变化,所以s1[0]...也随之不同
{
if(len<=0)
return;
s[len-1]=s1[0]; //把根结点添加到最后
int p=strchr(s2,s1[0])-s2; //找到根结点在中序遍历中的位置
build(p,s1+1,s2,s); //左子树的结点数为p
build(len-1-p,s1+p+1,s2+p+1,s+p); //右子树的结点数为len-1-p
}
int main()
{
char s1[MAXLEN],s2[MAXLEN],ans[MAXLEN];
while(scanf("%s%s",s1,s2)==2)
{
int len=strlen(s1);
build(len,s1,s2,ans);
ans[len]='\0';
printf("%s\n",ans);
}
return 0;
}

转载于:https://www.cnblogs.com/mjc467621163/archive/2011/07/22/2113905.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/80904.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息