数据结构上机测试4.1:二叉树的遍历与应用1

数据结构上机测试4.1:二叉树的遍历与应用1

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

输入二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列。

Input

第一行输入二叉树的先序遍历序列;
第二行输入二叉树的中序遍历序列。

Output

输出该二叉树的后序遍历序列。

Sample Input

ABDCEF
BDAECF

Sample Output

DBEFCA

把这两个字符串的左右分别定义为l1,r1,l2,r2,然后进行操作,那两个找子根的方法可以自己在纸上推演一下

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
char a[60];
char b[60];
int len;
void find_postorder_tree(int l1,int r1,int l2,int r2);
int main()
{
    scanf("%s%s",a,b);
    len=strlen(a);
    find_postorder_tree(0,len-1,0,len-1);
    printf("\n");
    return 0;
}
void find_postorder_tree(int l1,int r1,int l2,int r2)
{
    if(r1<l1||r2<l2)//结束标志是左大于右
    {
        return;
    }
    int pos=l2;
    while(a[l1]!=b[pos])
    {
        pos++;//在中序中找到根的位置
    }
    find_postorder_tree(l1+1,l1+(pos-l2),l2,pos-1);//找左子根
    find_postorder_tree(l1+(pos-l2)+1,r1,pos+1,r2);//找右子根
    printf("%c",b[pos]);
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值