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

题意:

后序中序链表建树,求层序

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n;
struct node{
int data;
node *left,*right;
};
int post[];
int in[];
vector<int>level;
queue<node*>q;
node *build(int pl,int pr,int il,int ir){
if(pl>pr || il>ir) return NULL;
int pos=-;
for(int i=il;i<=ir;i++){
if(in[i]==post[pr]){
pos=i;
break;
}
}
node *root=new node();
root->data=post[pr];
root->right=build(pr-(ir-pos),pr-,pos+,ir);//是ir-pos
root->left=build(pl,pr-(ir-pos)-,il,pos-);
return root;
}
int main(){
cin>>n;
for(int i=;i<=n;i++) cin>>post[i];
for(int i=;i<=n;i++) cin>>in[i];
node *root=build(,n,,n);
q.push(root);
while(!q.empty()){
root=q.front();
level.push_back(root->data);
q.pop();
if(root->left) q.push(root->left);
if(root->right) q.push(root->right);
}
for(int i=;i<level.size();i++){
cout<<level.at(i);
if(i!=level.size()-) 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 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)(25 分)

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

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

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

  5. PAT 甲级 1020 Tree Traversals

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

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

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

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

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

  8. 1020 Tree Traversals (25 分)

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

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

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

随机推荐

  1. 目标检测论文解读5——YOLO v1

    背景 之前热门的目标检测方法都是two stage的,即分为region proposal和classification两个阶段,本文是对one stage方法的初次探索. 方法 首先看一下模型的网络 ...

  2. 直接将字典转为DataFrame格式时,会出现:ValueError: If using all scalar values, you must pass an index(四种解决方案)

    问题:想要将字典直接转为DataFrame格式时,除了字典外没有传入其他参数时,会报错 ValueError: If using all scalar values, you must pass an ...

  3. SHOI2017 分手是祝愿

    分手是祝愿 有

  4. Excel——读取——导出目录

    /** * 导出Excel文件到具体的目录 * <一句话功能简述> * <功能详细描述> * @param fileName 导出的文件名 * @param sheetName ...

  5. 关于 Nginx 配置的一些疑惑, Nginx 根据cookie 进行rewrite

    网站目录结构如下:/public/en.html/public/zh_cn.html/public/index.php 之前所有的非静态资源请求都交给 index.php现在要把首页的请求 不走PHP ...

  6. (知识点3)附加到数字的“.f”的目的是什么?

    默认情况下3.2被视为double; 所以为了强制编译器将其视为float,你需要f在最后编写. float a = 3.2; if ( a == 3.2 ) cout << " ...

  7. planning algorithms chapter 3

    chapter 3 几何表示和变换 P.S: 总算到了 motion planning 部分了 几何建模 几何建模主要有两类方法:边界表示法和实体表示法. 环境模型可以是二维或三维,实体主要包括障碍物 ...

  8. ex2

    #include <stdio.h> int main() { int days; printf("输入一个整数: \n"); scanf("%d" ...

  9. AMPPZ2014 The Captain

    传送门 BZOJdown Solution 比较妙,考虑按照给出的式子,只有\(x\)相邻或者\(y\)相邻的才会走,不然一定会走到相邻的再走\(x\)或\(y\),所以直接排序两边然后最短路即可. ...

  10. Centos7 守护进程supervisord 安装使用

    pervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统.它 ...