数据结构上机测试4.1:二叉树的遍历与应用1
来源:互联网 发布:淘宝包含哪些部门 编辑:程序博客网 时间:2024/06/05 20:55
Problem Description
输入二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列。
Input
第一行输入二叉树的先序遍历序列;
第二行输入二叉树的中序遍历序列。
第二行输入二叉树的中序遍历序列。
Output
输出该二叉树的后序遍历序列。
Example Input
ABDCEFBDAECF
Example Output
DBEFCA
Hint
Author
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char data;
struct node *lchild,*rchild;
};
int search(char t[],char ch)
{
int i,l;
l=strlen(t);
for(i=0;i<l;i++)
{
if(t[i]==ch)
return i;
}
return 0;
}
struct node *creat(char s[],char t[],int ps,int in,int n)
{
int k;struct node *root;
if(n<=0)
root=NULL;
else
{
k=search(t,s[ps]);
if(k==-1)
root=NULL;
else
{
root=(struct node *)malloc(sizeof(struct node));
root->data=s[ps];
if(k==in)
root->lchild=NULL;
else
root->lchild=creat(s,t,ps+1,in,k-in);
if(k==in+n-1)
root->rchild=NULL;
else
root->rchild=creat(s,t,ps+1+(k-in),k+1,n-(k-in)-1);
}
}
return root;
}
void lastprint(struct node *root)
{
if(root)
{
lastprint(root->lchild);
lastprint(root->rchild);
printf("%c",root->data);
}
}
int main()
{
struct node *root;
char s[102],t[102];
int n;
scanf("%s",s);
scanf("%s",t);
n=strlen(s);
root=creat(s,t,0,0,n);
lastprint(root);
printf("\n");
return 0;
}
#include <stdlib.h>
#include <string.h>
struct node
{
char data;
struct node *lchild,*rchild;
};
int search(char t[],char ch)
{
int i,l;
l=strlen(t);
for(i=0;i<l;i++)
{
if(t[i]==ch)
return i;
}
return 0;
}
struct node *creat(char s[],char t[],int ps,int in,int n)
{
int k;struct node *root;
if(n<=0)
root=NULL;
else
{
k=search(t,s[ps]);
if(k==-1)
root=NULL;
else
{
root=(struct node *)malloc(sizeof(struct node));
root->data=s[ps];
if(k==in)
root->lchild=NULL;
else
root->lchild=creat(s,t,ps+1,in,k-in);
if(k==in+n-1)
root->rchild=NULL;
else
root->rchild=creat(s,t,ps+1+(k-in),k+1,n-(k-in)-1);
}
}
return root;
}
void lastprint(struct node *root)
{
if(root)
{
lastprint(root->lchild);
lastprint(root->rchild);
printf("%c",root->data);
}
}
int main()
{
struct node *root;
char s[102],t[102];
int n;
scanf("%s",s);
scanf("%s",t);
n=strlen(s);
root=creat(s,t,0,0,n);
lastprint(root);
printf("\n");
return 0;
}
#include <stdlib.h>
#include <string.h>
struct node
{
char data;
struct node *lchild,*rchild;
};
int search(char t[],char ch)
{
int i,l;
l=strlen(t);
for(i=0;i<l;i++)
{
if(t[i]==ch)
return i;
}
return 0;
}
struct node *creat(char s[],char t[],int ps,int in,int n)
{
int k;struct node *root;
if(n<=0)
root=NULL;
else
{
k=search(t,s[ps]);
if(k==-1)
root=NULL;
else
{
root=(struct node *)malloc(sizeof(struct node));
root->data=s[ps];
if(k==in)
root->lchild=NULL;
else
root->lchild=creat(s,t,ps+1,in,k-in);
if(k==in+n-1)
root->rchild=NULL;
else
root->rchild=creat(s,t,ps+1+(k-in),k+1,n-(k-in)-1);
}
}
return root;
}
void lastprint(struct node *root)
{
if(root)
{
lastprint(root->lchild);
lastprint(root->rchild);
printf("%c",root->data);
}
}
int main()
{
struct node *root;
char s[102],t[102];
int n;
scanf("%s",s);
scanf("%s",t);
n=strlen(s);
root=creat(s,t,0,0,n);
lastprint(root);
printf("\n");
return 0;
}
阅读全文
0 0
- 数据结构上机测试4.1:二叉树的遍历与应用
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 1291 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 【1291】数据结构上机测试4.1:二叉树的遍历与应用1 SDUTOJ
- 1291 数据结构上机测试4.1:二叉树的遍历与应用1
- SDUTOJ 1291 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1--知先序和中序求后序
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 数据结构上机测试4.1:二叉树的遍历与应用1 (*详解
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 【BLE4.0】CC254x-修改广播时间
- 微信小程序开发
- F
- Xamarin.Foms 用户界面——控件——Style——样式继承
- javascript深入理解js闭包
- 数据结构上机测试4.1:二叉树的遍历与应用1
- 2017年8月9日训练日记
- 刷题——6092 Rikka with Subset
- 5-4 畅通工程之最低成本建设问题 (30分)
- JavaScript作用域(一)
- Vim操作拾遗
- Mysql 开启视图权限与创建视图教程
- hdu1394最小逆序数——线段树
- bzoj 4710: [Jsoi2011]分特产 排列组合+容斥原理