树的重建

题目描述

        给出一棵二叉树的中序与后序排列。求出它的先序排列。(约定树结点用不同的大写字母表示,长度≤8)。

输入

第一行为二叉树的中序序列 第二行为二叉树的后序序列

输出

一行,为二叉树的先序序列

样例输入
BADC
BDCA
样例输出
ABCD



#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>

using namespace std;

char en[10],in[10];
void TREE(int en_sta,int en_end,int in_sta,int in_end)
{
    int k,range;
    char root;
    if(en_sta>en_end||in_sta>in_end)
        return ;
    if(en_sta==en_end)
    {
        cout<<en[en_sta];
        return ;
    }
    root=en[en_end];
    for(k=in_sta; k<=in_end; k++)
    {
        if(root==in[k])
            break;
    }
    range=k-in_sta;
    cout<<root;
    TREE(en_sta,en_sta+range-1,in_sta,k-1);
    TREE(en_sta+range,en_end-1,k+1,in_end);
}

int main()
{
    int len;
    cin>>in>>en;
       len=strlen(en);
    TREE(0,len-1,0,len-1);
    cout<<endl;
    return 0;
}

 

转载于:https://www.cnblogs.com/Asimple/p/5512113.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值