the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020

and the source code is as followed.

#include<iostream>
#include<cstdlib>
#include<queue> using namespace std; const int maxx = ; typedef struct Tree
{
Tree *le;
Tree *ri;
int data;
}Tree; Tree *root; int pos[maxx],in[maxx]; void printLevelOrder(Tree *root)
{
queue<Tree*> que;
Tree *tr = NULL;
que.push(root);
bool flg = true;
while (!que.empty())
{
tr = (Tree *)que.front();
que.pop();
if (tr == NULL) continue;
if (flg)
{
printf("%d",tr->data);
flg = false;
}else
{
printf(" %d",tr->data);
}
que.push(tr->le);
que.push(tr->ri);
}
printf("\n");
} //构造树pl为后序序列的左边界pr为其右边界
//il为中续遍历的左边界ir为其右边界
Tree *buildTree(int pl,int pr,int il,int ir)
{
if (pl > pr)return NULL;
int p = il;
while (in[p] != pos[pr])
{
++p;
}
Tree *tree = (Tree *)malloc(sizeof(Tree));
tree->data = pos[pr];
tree->le = buildTree(pl,pr-ir+p-,il,p-);
tree->ri = buildTree(pr-ir+p,pr-,p+,ir); return tree;
} int main(){
int n,i;
Tree *root; scanf("%d",&n);
for(i=;i<n;++i){
scanf("%d",&pos[i]);
}
for(i=;i<n;++i){
scanf("%d",&in[i]);
}
root=buildTree(,n-,,n-);
printLevelOrder(root);
return ;
}

the main function is rebuild the tree,which is a recursive function;and it is important to find

the left side and the right side from both postOrder and InOrder!Therefore ,we can use these argument to build the tree recursively.

1020. Tree Traversals (25)的更多相关文章

  1. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  2. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  3. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  4. PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  5. 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

  6. 【PAT】1020. Tree Traversals (25)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. 1020. Tree Traversals (25) -BFS

    题目如下: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...

  8. 1020 Tree Traversals (25)(25 point(s))

    problem Suppose that all the keys in a binary tree are distinct positive integers. Given the postord ...

  9. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

随机推荐

  1. TiD大会学习心得之沟通交流

    沟通交流是敏捷成功的关键要素,据相关调查阻碍敏捷的主要原因都与人有关:例如缺乏领导支持.团队缺乏协作精神.公司文化与敏捷相冲突等等:同时沟通交流也是团队建设.教练自身成长的重要支柱.下面结合在< ...

  2. nginx+uwsgi+django+celery+supervisord环境部署

    前言 很久没更博客了,最近新写了一个小项目,后边有时间把一些心得放上来,先把环境的部署方式整理出来. 部署过程 先将环境的python升级为2.7 保证有pip 安装了nginx并配置 vim /Da ...

  3. np问题

    NP(np) Time Limit:1000ms Memory Limit:64MB 题目描述 LYK 喜欢研究一些比较困难的问题,比如 np 问题.这次它又遇到一个棘手的 np 问题.问题是这个样子 ...

  4. Java基础之写文件——创建通道并且写文件(TryChannel)

    控制台程序,创建一个文件并且使用通道将一些文本写入到这个文件中. import static java.nio.file.StandardOpenOption.*; import java.nio.c ...

  5. 使用jQuery设置disabled属性与移除disabled属性

    Readonly只针对input和textarea有效,而disabled对于所有的表单元素都有效,下面为大家介绍下使用jQuery设置disabled属性   表单中readOnly和disable ...

  6. 运行ipython后显示WARNING: IPython History requires SQLite, your history will not be saved

    在CentOS6.5下将自带的python2.6升级到python2.7,并安装了ipython,启动ipython后显示如下信息: WARNING: IPython History requires ...

  7. hdu 3094 A tree game 博弈论

    思路: 叶子节点的SG值为0:中间节点的SG值为它的所有子节点的SG值加1 后的异或和. 详见贾志豪神牛的论文:组合游戏略述 ——浅谈SG游戏的若干拓展及变形 代码如下: #include<cs ...

  8. Java验证码和ajax判断

    关于来了解相关的api BufferedImage(int width, int height, int imageType) 构造一个类型为预定义图像类型之一的 BufferedImage. Buf ...

  9. SpringBoot自定义拦截器实现IP白名单功能

    SpringBoot自定义拦截器实现IP白名单功能 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8993331.html 首先,相关功能已经上线了,且先让我先 ...

  10. Jmeter—实现识别验证码登录

    在做自动化测试或压力测试时,验证码总是一个问题.在以往的压力测试经历中,测试一般在独立的测试环境中进行,可以放心禁用验证码或使用万能验证码,这个是最实用的.但是,这两天我尝试了一个使用第三方的图形图像 ...