码迷,mamicode.com
首页 > 其他好文 > 详细

求先序排列

时间:2017-03-01 21:18:49      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:bcd   flask   不同   stream   bdc   ace   icon   esc   pre   

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
题目描述 Description

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

输入描述 Input Description

两个字符串,分别是中序和后序(每行一个)

输出描述 Output Description

一个字符串,先序

样例输入 Sample Input

BADC

BDCA

样例输出 Sample Output

ABCD

数据范围及提示 Data Size & Hint

 

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

string zx,hx;

void get_hx(int l1,int r1,int l2,int r2)
{
    int m=zx.find(hx[r2]);
    cout<<hx[r2];
    if(m>l1)
    get_hx(l1,m-1,l2,l2+m-1-l1);
    if(m<r1)
    get_hx(m+1,r1,l2+m-l1,r2-1);
    
}

int main()
{
    cin>>zx>>hx;
    
    int r1=zx.length()-1;
    int r2=hx.length()-1;
    get_hx(0,r1,0,r2);
    
    return 0;
}

 

求先序排列

标签:bcd   flask   不同   stream   bdc   ace   icon   esc   pre   

原文地址:http://www.cnblogs.com/Shy-key/p/6485968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!