//典型后中省树,这种方法必须有 中序序列来确定根的位置,然后二分建树;

//因为用的vc,之前用序列位置建树通不过,用坐标建树通过了,怀疑vc的功能限制,有时间再来测试,眼下感觉还是坐标好啊,用地址巧了半个晚上了找不着八哥,还不A,哭晕

#include<cstdio>
//#include<cstring>
#include<queue>
#include<vector>
//#include<algorithm>
using namespace std;
const int maxn=31;
struct node
{
int data;
node *lchild,*rchild;
};
int pre[maxn],post[maxn],in[maxn];
int n;
queue<node*>q;
vector<int>level;
node *create(int postl,int postr,int inl,int inr)
{
if(postl>postr)return NULL;
node* root=new node;
root->data=post[postr];
root->lchild=root->rchild=NULL;
int k;
for(k=inl;k<=inr;k++)
if(in[k]==post[postr])break;
int numleft=k-inl;
root->lchild=create(postl,postl+numleft-1,inl,k-1);
root->rchild=create(postl+numleft,postr-1,k+1,inr);
return root;
}
void bfs(node *root)
{
if(root!=NULL)q.push(root);
while(!q.empty())
{
node *tmp=q.front();q.pop();
level.push_back(tmp->data);
if(tmp->lchild!=NULL)q.push(tmp->lchild);
if(tmp->rchild!=NULL)q.push(tmp->rchild);
}
}
int main()
{
freopen("input.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
int i;
for( i=0;i<n;i++)scanf("%d",&post[i]);
for( i=0;i<n;i++)scanf("%d",&in[i]);
node *root=create(0,n-1,0,n-1);
bfs(root);
for( i=0;i<level.size();i++)
{
if(i==level.size()-1)printf("%d\n",level[i]);
else printf("%d ",level[i]);
}
}
return 0;
}

PAT1020. Tree Traversals的更多相关文章

  1. pat1020. Tree Traversals (25)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  2. PAT1086:Tree Traversals Again

    1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  3. Tree Traversals

    Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...

  4. HDU 1710 二叉树的遍历 Binary Tree Traversals

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. hdu1710(Binary Tree Traversals)(二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. HDU1710Binary Tree Traversals

    HDU1710Binary Tree Traversals 题目大意:给一个树的前序遍历和中序遍历,要求输出后序遍历. (半年前做这道题做了两天没看懂,今天学了二叉树,回来AC了^ ^) 首先介绍一下 ...

  7. HDU-1701 Binary Tree Traversals

    http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Tr ...

  8. 03-树2. Tree Traversals Again (25)

    03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  9. HDU 1710-Binary Tree Traversals(二进制重建)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. .net EntityFramework用法探索系列 1

    EntityFramework用法探索系列 (一)DatabaseFirst (二)CodeFirst (三)CodeFirst流畅API (四)Repository和UnitOfWork (五)引入 ...

  2. 开发高峰时的CPU使用率

  3. windows下boost库的基本使用方法

    因为boost都是使用模板的技术,所以所有代码都是写在一个.hpp头文件中.这样boost中的大部分内容是不需要编译生成相应的链接库,只需要设置下面的包含目录(或者设置一下环境变量),在源文件中包含相 ...

  4. sublime运行c++快捷建修改

    打开preferences->key bingings -user 输入 [ {"keys": ["f9"], "command": ...

  5. CF478 B. Random Teams 组合数学 简单题

    n participants of the competition were split into m teams in some manner so that each team has at le ...

  6. CF 461B Appleman and Tree 树形DP

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

  7. C# Bitmap Save Generic GDI+ Error

    Image.Save 方法 (String) 将该 Image 保存到指定的文件或流. 命名空间:  System.Drawing程序集:  System.Drawing(在 System.Drawi ...

  8. mac下Android开发环境搭建

    之前一段时间在学习ios的开发,近一段时间想着也接触下Android开发,以来加深对移动端开发的理解.这里根据自己配置Android开发环境的过程,比较详细的来总结下自己的安装过程,希望对一些正准备配 ...

  9. substr

    substr(string,start,length) string - 指定的要截取的字符串 start - 必需,规定在字符串的何处开始 正数 - 在字符串的指定位置开始 负数 - 在从字符串结尾 ...

  10. linux内核设计与实现--从内核出发

    linux内核有两种版本:稳定的和处于开发中的. linux通过一种简单的命名机制来区分稳定的和处于开发中的内核,使用3个或者4个“.”分割的数字来代表不同内核版本. 如:2.6.26.1:第一个数字 ...