【已知先序、中序求后序排列】——字符串类型

#1049 : 后序遍历

时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
小Ho在这一周遇到的问题便是:给出一棵二叉树的前序和中序遍历的结果,还原这棵二叉树并输出其后序遍历的结果。
提示:分而治之——化大为小,化小为无
输入
每个测试点(输入文件)有且仅有一组测试数据。
每组测试数据的第一行为一个由大写英文字母组成的字符串,表示该二叉树的前序遍历的结果。
每组测试数据的第二行为一个由大写英文字母组成的字符串,表示该二叉树的中序遍历的结果。
对于100%的数据,满足二叉树的节点数小于等于26。
输出
对于每组测试数据,输出一个由大写英文字母组成的字符串,表示还原出的二叉树的后序遍历的结果。
样例输入
AB
BA
样例输出
BA

【分析】:在注释里面。

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e3 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string a,b;
string dfs(int l1,int l2,int len)//l1前序起点、l2中序起点、len是树的总长
{
int i;
if(len<=0) return "";
for(i=l2;i<l2+len;i++)
{
if(b[i]==a[l1])
break;//用i记录当前根节点
}
int cnt=i-l2;//左子树遍历的长度
string a1 = dfs(l1+1,l2,cnt);//递归左子树,左子树先序遍历起始点为l1+1,左子树中序遍历起始点始终为l2
string a2 = dfs(l1+cnt+1,i+1,len-cnt-1);//递归右子树,右子树先序遍历起始点在左子树右侧为l1+cnt+1,右子树中序遍历始终在根节点i右侧为i+1
return a1+a2+a[l1];//由于是后序遍历 层数最低的根节点放置末尾
} int main()
{
while(cin>>a>>b)
{
cout<< dfs(0,0,a.length()) << endl;
}
}

(已知前序、中序求后序)[http://acm.hdu.edu.cn/showproblem.php?pid=1710]——【int数组类型】

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e3 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int pre[maxn],mid[maxn],post[maxn],k=0;
//给一棵树的前序,中序输出后序
/*
先序遍历的第一个节点一定是这棵树上的根节点,
那么在中序遍历里找到根节点,
中序数组根节点左边的元素全在左子树,
右边的元素在右子树,一直划分到叶子节点为止。
*/
void dfs(int l1,int r1,int l2,int r2)
{
if(l1>r1 || l2>r2) return ;
int o = pre[l1], pos;
for(int i=l2; ;i++)
{
if(mid[i] == o)
{
pos = i;
break;
}
}
int cnt = pos - l2;//左子树大小
dfs(l1+1,l1+cnt, l2,pos-1); //左子树->前+中
dfs(l1+cnt+1,r1, pos+1,r2); //右子树->前+中
post[k++]=o;
} int n;
int main()
{
while(~scanf("%d",&n))
{
k=0;
for(int i=1;i<=n;i++)
scanf("%d",&pre[i]);
for(int i=1;i<=n;i++)
scanf("%d",&mid[i]);
dfs(1,n,1,n);
for(int i=0;i<k;i++)
printf("%d%c",post[i],i==k-1?'\n':' ');
}
}
/*
9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
7 4 2 8 9 5 6 3 1
*/

洛谷:

【已知中序、后序求先序】——字符串类型:

题目描述

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

输入输出格式

输入格式:

22 行,均为大写字母组成的字符串,表示一棵二叉树的中序与后序排列。

输出格式:

11 行,表示一棵二叉树的先序。

输入输出样例

输入样例#1:

BADC

BDCA

输出样例#1:

ABCD

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e3 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char pre[maxn],mid[maxn],post[maxn];
int k=0;
vector<char> v;
//给一棵树的中序、后序输出 求先序排列[int]
void dfs(int l1,int r1,int l2,int r2)
{
if(l1>r1) return;
int o = post[r2], pos;
v.push_back(o);
for(int i=l1; ;i++)
{
if(mid[i] == o)
{
pos = i;
break;
}
}
int cnt = pos - l1;//左子树大小
dfs(l1,pos-1, l2,l2+cnt-1);//左子树->中+后
dfs(pos+1,r1, l2+cnt,r2-1);//右子树->中+后
//pre[k++]=o;
} int n;
int main()
{
while(~scanf("%s%s",mid,post))
{
v.clear();
k=0;
int n = strlen(mid);
dfs(0,n-1,0,n-1);
for(int i=0;i<v.size();i++)
{
printf("%c",v[i]);
}
cout<<endl;
}
}
/*
4
2 1 4 3
2 4 3 1
*/

【已知中序、后序求先序】——int类型

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e3 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int pre[maxn],mid[maxn],post[maxn],k=0;
vector<int> v;
//给一棵树的中序、后序输出 求先序排列[int]
void dfs(int l1,int r1,int l2,int r2)
{
if(l1>r1) return;
int o = post[r2], pos;
v.push_back(o);
for(int i=l1; ;i++)
{
if(mid[i] == o)
{
pos = i;
break;
}
}
int cnt = pos - l1;//左子树大小
dfs(l1,pos-1, l2,l2+cnt-1);//左子树->中+后
dfs(pos+1,r1, l2+cnt,r2-1);//右子树->中+后
//pre[k++]=o;
} int n;
int main()
{
while(~scanf("%d",&n))
{
v.clear();
k=0;
for(int i=1;i<=n;i++)
scanf("%d",&mid[i]);
for(int i=1;i<=n;i++)
scanf("%d",&post[i]);
dfs(1,n,1,n);
for(int i=0;i<v.size();i++)
{
if(i!=v.size()-1)
printf("%d ",v[i]);
else printf("%d\n",v[i]);
}
}
}
/*
4
2 1 4 3
2 4 3 1
*/

如何求先序排列和后序排列——hihocoder1049+洛谷1030+HDU1710+POJ2255+UVA548【二叉树递归搜索】的更多相关文章

  1. Tree Traversals Again(根据前序,中序,确定后序顺序)

    题目的大意是:进行一系列的操作push,pop.来确定后序遍历的顺序 An inorder binary tree traversal can be implemented in a non-recu ...

  2. 已知树的前序、中序,求后序的java实现&已知树的后序、中序,求前序的java实现

    public class Order { int findPosInInOrder(String str,String in,int position){ char c = str.charAt(po ...

  3. DS Tree 已知先序、中序 => 建树 => 求后序

    参考:二叉树--前序和中序得到后序 思路历程: 在最初敲的时候,经常会弄混preorder和midorder的元素位置.大体的思路就是在preorder中找到根节点(根节点在序列的左边),然后在mid ...

  4. lintcode: 中序遍历和后序遍历树构造二叉树

    题目 中序遍历和后序遍历树构造二叉树 根据中序遍历和后序遍历树构造二叉树 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 /  \ 1    3 注意 你可 ...

  5. java编写二叉树以及前序遍历、中序遍历和后序遍历 .

    /** * 实现二叉树的创建.前序遍历.中序遍历和后序遍历 **/ package DataStructure; /** * Copyright 2014 by Ruiqin Sun * All ri ...

  6. python数据结构之树和二叉树(先序遍历、中序遍历和后序遍历)

    python数据结构之树和二叉树(先序遍历.中序遍历和后序遍历) 树 树是\(n\)(\(n\ge 0\))个结点的有限集.在任意一棵非空树中,有且只有一个根结点. 二叉树是有限个元素的集合,该集合或 ...

  7. 根据 中序遍历 和 后序遍历构造树(Presentation)(C++)

    好不容易又到周五了,周末终于可以休息休息了.写这一篇随笔只是心血来潮,下午问了一位朋友PAT考的如何,顺便看一下他考的试题,里面有最后一道题,是关于给出中序遍历和后序遍历然后求一个层次遍历.等等,我找 ...

  8. 51Nod 算法马拉松28 A题 先序遍历与后序遍历 分治

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - 51Nod1832 题意概括 对于给定的一个二叉树的先序遍历和后序遍历,输出有多少种满足条件的二叉树. 两棵二 ...

  9. TZOJ 3209 后序遍历(已知中序前序求后序)

    描述 在数据结构中,遍历是二叉树最重要的操作之一.所谓遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问. 这里给出三种遍历算法. 1.中序遍历的递归算法定义:  ...

随机推荐

  1. 【Android】完善Android学习(一:API 2.3.3)

    备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...

  2. MySQL查看所有用户及拥有权限

    查看MYSQL数据库中所有用户 mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM m ...

  3. [洛谷P3763] [TJOI2017]DNA

    洛谷题目链接:[TJOI2017]DNA 题目描述 加里敦大学的生物研究所,发现了决定人喜不喜欢吃藕的基因序列S,有这个序列的碱基序列就会表现出喜欢吃藕的性状,但是研究人员发现对碱基序列S,任意修改其 ...

  4. szoj657 【AHSDFZNOI 7.2 WuHongxun】Odd

    [题目大意] 给出$n$个数$a_1, a_2, ..., a_n$,求有多少个区间$[l, r]$,满足每个数都出现了奇数次. $1 \leq n \leq 2 * 10^5, 0 \leq a_i ...

  5. UOJ#31 【UR #2】猪猪侠再战括号序列

    传送门http://uoj.ac/problem/31 大家好我是来自百度贴吧的_叫我猪猪侠,英文名叫_CallMeGGBond. 我不曾上过大学,但这不影响我对离散数学.复杂性分析等领域的兴趣:尤其 ...

  6. 【51NOD-0】1085 背包问题

    [算法]背包DP [题解]f[j]=(f[j-w[i]]+v[i]) 记得倒序(一个物品只能取一次) #include<cstdio> #include<algorithm> ...

  7. Spring Cloud的基本认识和使用Spring Cloud的基本教程(山东数漫江湖)

    认识Spring Cloud Spring Cloud简单来说就是一个微服务相关的框架,至于什么是微服务,简单来说就是一个整体项目由多个单独运行的小项目构成,每个小项目负责一个或多个功能,每个小项目有 ...

  8. HDU 2717 Catch That Cow (深搜)

    题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...

  9. LCD实验学习笔记(八):中断

    s3c2440有60个中断源(其中15个为子中断源). 31个32位的通用寄存器,6个程序状态寄存器.有6种工作模式(系统/用户模式,快中断模式,管理模式,数据访问中止模式,中断模式,未定指令中止模式 ...

  10. python 正则表达式口诀

    正则其实也势利,削尖头来把钱揣: (指开始符号^和结尾符号$)   特殊符号认不了,弄个倒杠来引路: (指\. \*等特殊符号)   倒杠后面跟小w, 数字字母来表示: (\w跟数字字母;\d跟数字) ...