给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历

#include<bits/stdc++.h>

using namespace std;
const int N=;
int in[N];
int post[N]; typedef struct node;
typedef node *tree;
struct node
{
int data;
tree L;
tree R;
};
void print(tree &bt,int l1,int r1,int l2,int r2)
{
if(l1>r1||l2>r2) return;
int mid=l2;
while(in[mid]!=post[r1]) mid++;
bt=new node;
bt->data=post[r1];
bt->L=NULL;
bt->R=NULL;
print(bt->L,l1,l1+mid-l2-,l2,mid-);
print(bt->R,l1+mid-l2,r1-,mid+,r2);
}
vector<int>p;
void s(tree bt)
{
queue<tree>Q;
Q.push(bt);
while(!Q.empty()){
tree u=Q.front();
Q.pop();
p.push_back(u->data);
if(u->L) Q.push(u->L);
if(u->R) Q.push(u->R);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d",&post[i]);
for(int i=;i<n;i++) scanf("%d",&in[i]);
tree bt;
print(bt,,n-,,n-);
s(bt);
for(int i=;i<p.size();i++){
if(i) printf(" ");
printf("%d",p[i]);
}
printf("\n");
return ;
}

1020 Tree Traversals (25 分)(二叉树的遍历)的更多相关文章

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

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

  2. PAT Advanced 1020 Tree Traversals (25 分)

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

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

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

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

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

  5. 1020 Tree Traversals (25分)思路分析 + 满分代码

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

  6. 1020 Tree Traversals (25 分)

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

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

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

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

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

  9. 1020. Tree Traversals (25) ——树的遍历

    //题目 通过后续遍历 中序遍历 得出一棵树 ,然后按树的层次遍历打印 PS:以前对于这种用指针的题目是比较头痛的,现在做了一些链表操作后,感觉也不难 先通过后续中序建一棵树,然后通过BFS遍历这棵树 ...

  10. 1020. Tree Traversals (25) -BFS

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

随机推荐

  1. eclips新建Maven Web项目

    一.创建项目 1.Eclipse中用Maven创建项目 上图中Next 2.继续Next 3.选maven-archetype-webapp后,next 4.填写相应的信息,Packaged是默认创建 ...

  2. 百度Ueditor 图片上传无反应,显示上传0张,不能点确定

    解决办法: \Data\Ueditor\php\Uploader.class.php 190行左右 /** * 获取文件扩展名 * @return string */ private function ...

  3. PHP精度问题

    PHP 为任意精度数学计算提供了二进制计算器(Binary Calculator),它支持任意大小和精度的数字,以字符串形式描述 bcadd — 加法bccomp — 比较bcdiv — 相除bcmo ...

  4. P3901 【数列找不同】

    这个题我们可以使用树状数组做 啥? 树状数组? 那个不是维护前缀和的东西吗? 各位看官,让我慢慢道来. 首先我们可以想到,对于一个询问$ [l,r] \(,只有\)[1,r]$中的数可能对这个询问有影 ...

  5. parameter server

    http://zeromq.org ZeroMQ \zero-em-queue\, \ØMQ\: Ø  Connect your code in any language, on any platfo ...

  6. JT796、JT1077部标平台检测报名须知

    检测报名须知 申请道路运输车辆卫星定位系统平台标准符合性检测时,请先将1检测意向单(只针对企业监控平台).2符合性检测申请材料(基本材料包括:申请函.授权人身份证复印件.检测登记表.运输企业信息表.平 ...

  7. java基础必备单词讲解 day five

    Rectangle width high height area employee tool param version author math guess resources 之前单词复习 path ...

  8. 【思维题 细节】loj#6042. 「雅礼集训 2017 Day7」跳蚤王国的宰相

    挂于±1的细节…… 题目描述 跳蚤王国爆发了一场动乱,国王在镇压动乱的同时,需要在跳蚤国地方钦定一个人来做宰相. 由于当时形势的复杂性,很多跳蚤都并不想去做一个傀儡宰相,带着宰相的帽子,最后还冒着被打 ...

  9. python错误处理之try...except...finally...错误处理机制。

    今天学习了python的错误处理. 在程序处理的过程中,经常会出现错误,一旦出错就会非常麻烦.所以有的高级语言通常都内置了一套  try...exept...finaly...的错误处理机制,pyth ...

  10. php 多维数组相同键值处理合并

    一.前言 在实际情况中,有时需要针对多维数组相同键值作相应的处理(四则运算.比较大小等)后才能够使用到实际情况中,现给出三维数组(多维数组可相应拓展)任意多个相同键值处理的函数,以备查阅. 二.代码 ...