1020. Tree Traversals (25)

时间:2022-08-26 22:18:48

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 &lpar;25&rpar;(25 分)

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

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

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

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

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

  4. PAT Advanced 1020 Tree Traversals &lpar;25 分&rpar;

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

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

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

  6. 【PAT】1020&period; Tree Traversals &lpar;25&rpar;

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

  7. 1020&period; Tree Traversals &lpar;25&rpar; -BFS

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

  8. 1020 Tree Traversals &lpar;25&rpar;(25 point&lpar;s&rpar;)

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

  9. PAT Advanced 1020 Tree Traversals &lpar;25&rpar; &lbrack;⼆叉树的遍历,后序中序转层序&rsqb;

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

随机推荐

  1. &lbrack;翻译&rsqb;AKKA笔记 - CHILD ACTORS与ACTORPATH -6

    原文:http://rerun.me/2014/10/21/akka-notes-child-actors-and-path/ Actor是完全的继承结构.你创建的任何Actor肯定都是一个其他Act ...

  2. PMP考试--关于职业道德

    如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 ★四个价值标准(value standards) 责任(responsibility ...

  3. 跨越跳板机传文件nc

    从线上服务器与本机互传文件 传输方 nc -l 10000 < a.tar 接收方 nc xx.xx.xx.xx 10000 >a.tar 原理: 文件传输方运行nc,指定端口,设置监听文 ...

  4. EventBus 事件总线 案例

    简介 地址:https://github.com/greenrobot/EventBus EventBus是一个[发布 / 订阅]的事件总线.简单点说,就是两人[约定]好怎么通信,一人发布消息,另外一 ...

  5. Java基础系列--包装类

    原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/5462489.html 1.8种基本数据类型都有各自的包装类,其对应关系为: 基本—————— ...

  6. 死锁问题分析&lpar;个人认为重点讲到了gap间隙锁,解决了我一些不明报死锁的问题&rpar;

    线上某服务时不时报出如下异常(大约一天二十多次):“Deadlock found when trying to get lock;”. Oh, My God! 是死锁问题.尽管报错不多,对性能目前看来 ...

  7. NOIP2018联赛总结

    NOIP2018联赛总结 Day 0 打了几个模板,看了一下别人的博客,背了一下vimrc Day 1 到了考场,先把vimrc配好 打开题目一先把三道题瞄了一眼,\(T1\)似乎是NOIP原题,\( ...

  8. 【原创】大数据基础之Airflow(2)生产环境部署airflow研究

    一 官方 airflow官方分布式部署结构图 airflow进程 webserver scheduler flower(非必须) worker airflow缺点 scheduler单点 通过在sch ...

  9. python中in,not in,比较运算符,格式化输出,编码

    一,python中的in,和not in python中in的作用是检测或查找,例如: c = ‘你好大号胡覅但是啊飞碟说’ b = ‘你好’ print(b in c ) 结果: True c = ...

  10. dokuwiki编辑器修改-color插件-添加按钮

    需求 dokuwiki的编辑工具栏是以 MediaWiki 的为基础发展来的. 在它的编辑器color插件的颜色按钮中,我想添加新的按钮功能.如红色字体黄色背景的修饰,类似于涂中文字强调的意思. 步骤 ...