1020 Tree Traversals (25 分)
 

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

这个推了蛮久的。。。。。不太熟练

    //左子树
if(k-l2==){
tree[root].l = -;//左子树为空
}
else{
buildTree(*root,l1,l1+k-l2-,l2,k-);
}
//右子树
if(k+>r2){
tree[root].r = -;//右子树为空
}
else{
buildTree(*root+,l1+k-l2,r1-,k+,r2);
}

AC代码:

#include<bits/stdc++.h>
using namespace std;
int post[];
int in[];
int n;
struct node{
int v;
int l;
int r;
}tree[];
queue<int>q;
void buildTree(int root,int l1,int r1,int l2,int r2){//后序,中序
//cout<<root<<" "<<l1<<"-"<<r1<<" "<<l2<<"-"<<r2<<endl;
//先找根节点
tree[root].v = post[r1];
if(l1==r1){ //只有一个节点
tree[root].l = -;
tree[root].r = -;
return;
}else{
tree[root].l = *root;
tree[root].r = *root+;
}
//找一下根在中序上的位置
int k;
for(int i=l2;i<=r2;i++){
if(in[i]==post[r1]){
k=i;//前面k-l2个数就是左子树
break;
}
}
//左子树
if(k-l2==){
tree[root].l = -;//左子树为空
}
else{
buildTree(*root,l1,l1+k-l2-,l2,k-);
}
//右子树
if(k+>r2){
tree[root].r = -;//右子树为空
}
else{
buildTree(*root+,l1+k-l2,r1-,k+,r2);
}
}
int main(){
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>post[i];
}
for(int i=;i<=n;i++){
cin>>in[i];
}
buildTree(,,n,,n);
//bfs 层序
while(!q.empty()) q.pop();
q.push();
while(!q.empty()){
node x=tree[q.front()];
q.pop();
cout<<x.v;
if(x.l!=-){
q.push(x.l);
}
if(x.r!=-){
q.push(x.r);
}
if(!q.empty()){
cout<<" ";
}
}
return ;
}

PAT 甲级 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 intege ...

  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 Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

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

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

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

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

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

  6. PAT 甲级 1020 Tree Traversals (二叉树遍历)

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

  7. PAT 甲级 1020 Tree Traversals

    https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...

  8. 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 分)

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

随机推荐

  1. python自动生成Docx(docxtpl库)

    python这个库很有用,可以格式化生成报告等. 其他内容请点此处,下面只写docxtpl的功能代码. # coding: utf-8 import web # 我们用的webpy框架 import ...

  2. SQL性能优化思路

    1. 尽可能把数据的存储和计算放入Memory而不是Disk,且减少IO操作,比如运用Redis等缓存技术 2. 对数据表进行精心设计,特别是大数据表,对常用数据字段进行适当的冗余,尽可能避免分表导致 ...

  3. sql 发生死锁

    SELECT request_session_id spid , OBJECT_NAME(resource_associated_entity_id) tableName FROM sys.dm_tr ...

  4. JavaScript 中call()、 apply()、 bind()改变this指向理解

    最近开发的过程中遇到了this指向问题,首先想到的是call().apply().bind()三个方法,有些时候这三个方法确实是十分重要,现在我们就把他们的使用方法及异同点讲解一下. 1.每个函数都包 ...

  5. JAVA遇见HTML——JSP篇(1、JAVA WEB简介)

    比如淘宝.新浪.搜狐.网易就是Web应用程序

  6. [2019牛客多校第四场][G. Tree]

    题目链接:https://ac.nowcoder.com/acm/contest/884/G 题目大意:给定一个树\(A\),再给出\(t\)次询问,问\(A\)中有多少连通子图与树\(B_i\)同构 ...

  7. Codeforces Round #469 (Div. 1) 949C C. Data Center Maintenance (Div. 2 950E)

    题 OvO http://codeforces.com/contest/949/problem/C codeforces 949C 950E 解 建图,记原图为 G1,缩点,记缩完点后的新图为G2 缩 ...

  8. 004——转载C#禁止改变窗体大小

    原文链接:http://www.cnblogs.com/shaozhuyong/p/5545005.html 1.先把MaximizeBox和MinimumBox设置为false,这时你发现最大最小化 ...

  9. Base64().encodeBase64Chunked导致换行符的问题

    String linkStr=new String(new Base64().encodeBase64Chunked(new String("conferid="+cid+&quo ...

  10. ZOJ 3182 HDU 2842递推

    ZOJ 3182 Nine Interlinks 题目大意:把一些带标号的环套到棍子上,标号为1的可以所以操作,标号i的根子在棍子上时,只有它标号比它小的换都不在棍子上,才能把标号为i+1的环,放在棍 ...