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. 最长上生子序列LIS

    学习动态规划问题(DP问题)中,其中有一个知识点叫最长上升子序列(longest  increasing subsequence),也可以叫最长非降序子序列,简称LIS.简单说一下自己的心得. 我们都 ...

  2. IoT设备上的恶意软件——通过漏洞、弱密码渗透

    2018年,是 IoT 高速发展的一年,从空调到电灯,从打印机到智能电视,从路由器到监控摄像头统统都开始上网.随着5G网络的发展,我们身边的 IoT 设备会越来越多.与此同时,IoT 的安全问题也慢慢 ...

  3. IDEA+Maven+Mybatis 巨坑:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rao.mapper.UserMapper.findAll

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rao.mapper.User ...

  4. Longest Common Substring II SPOJ - LCS2 (后缀自动机)

    Longest Common Substring II \[ Time Limit: 236ms\quad Memory Limit: 1572864 kB \] 题意 给出\(n\)个子串,要求这\ ...

  5. SHOI做题记录

    LOJ #2027. 「SHOI2016」黑暗前的幻想乡 考虑到每个公司一条边,那就等价于没有任何一家公司没有边. 然后就可以容斥+矩阵树定理,没了. LOJ #2028. 「SHOI2016」随机序 ...

  6. shell 求数组的平均值,求和,最大值,最小值

    test.sh #!/bin/bash arr=( ) let min=${arr[]} let max=${min} sum= ;i<${#arr[*]};i++)) do [[ ${min} ...

  7. AngularJS实现地址栏取值

    有时候我们由如下需求 1.从a.html跳转到b.html 2.从a跳转时携带参数和值. 3.从b.html中取出传过来的参数值 在AngularJS的操作如下 在a.html中添加 <a hr ...

  8. 解决linux环境下nohup: redirecting stderr to stdout问题

    在生产环境下启动Weblogic时,发现原来好好的nohup信息输出到指定文件中的功能,突然出问题了.现象是控制台输出的信息一部分输出到了我指定的文件,另一部分却输出到了nohup.out,而我是不想 ...

  9. web: 屏蔽button元素的空格键和enter响应

    用tab键可以在各种元素之间切换焦点,然后键盘上的空格和enter按键触发click事件,我这里不想要这种效果. 所以我需要屏蔽这两个按键. function keyup(e) { var currK ...

  10. 如何在Docker容器之间拷贝数据

    [编者的话]在容器之间拷贝数据是Docker一个重要而且基本的功能.拷贝数据到其他容器是一个经常使用到的场景,如当服务器遇到不可预见的“灾难”(注:断电,宕机)时,起到备份数据的作用.本文作者详细介绍 ...