给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。

输入格式:

输入第一行给出一个正整数N(<=30),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。

输出格式:

在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:

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

输出样例:

4 1 6 3 5 7 2

题目很简单,就是已知后序中序求层序。其实和已知后序中序求前序一样的做法,只要将求得的序列用数组保存起来,用数组存起来的序列就是层序遍历的结果。

//Asimple
#include <bits/stdc++.h>
#define INF 1e10
#define mod 10007
#define swap(a,b,t) t = a, a = b, b = t
#define CLS(a, v) memset(a, v, sizeof(a))
#define debug(a) cout << #a << " = " << a <<endl
#define abs(x) x<0?-x:x
using namespace std;
typedef long long ll;
const int maxn = ;
int n;
int pos[maxn], in[maxn];
vector<int> res(, -); void solve(int root, int s, int e, int index) {
if( s > e ) return ;
int i = s;
while( i<e && in[i]!=pos[root])i++;
res[index] = pos[root];
solve(root--e+i, s, i-, index*+);
solve(root-, i+, e, index*+);
} void input() {
cin >> n;
for(int i=; i<n; i++) cin >> pos[i] ;
for(int i=; i<n; i++) cin >> in[i];
solve(n-, , n-, );
int cnt = ;
for(int i=; i<res.size(); i++) {
if( res[i]!=- && cnt!=n- ) {
printf("%d ", res[i]);
cnt ++;
} else if(res[i]!=-){
printf("%d\n", res[i]);
break;
}
}
} int main() {
input();
return ;
}

pat 团体赛练习题集 L2-006. 树的遍历的更多相关文章

  1. pat 团体赛练习题集 L2-008. 最长对称子串

    对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...

  2. pat 团体赛练习题集 L2-007. 家庭房产

    给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下列格式给出一个人的房产: ...

  3. PAT甲级——1094 The Largest Generation (树的遍历)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93311728 1094 The Largest Generati ...

  4. PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]

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

  5. GPTL—练习集—006树的遍历

    #include<bits/stdc++.h> using namespace std; typedef int daTp;//datatype typedef struct BTNode ...

  6. PAT甲级专题|树的遍历

    PAT甲级专题-树的遍历 涉及知识点:树.建树.深度优先搜索.广度优先搜索.递归 甲级PTA 1004 输出每一层的结点,邻接表vector建树后.用dfs.bfs都可以边搜边存当前层的数据, #in ...

  7. pat L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  8. pat 团体天梯赛 L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  9. 天梯 L2 树的遍历(已知后序中序求层序)

    树的遍历 (25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数.第二行 ...

随机推荐

  1. 112A

    #include <iostream> #include <string> #include <cctype> using namespace std; int m ...

  2. Java创建文件夹

    import java.io.File; public class Mkdirs { public static void main(String[] args) { /** *创建文件夹,如果路径不 ...

  3. char varchar

    对于字符类型的有:char:固定长度,存储ANSI字符,不足的补英文半角空格.nchar:固定长度,存储Unicode字符,不足的补英文半角空格varchar:可变长度,存储ANSI字符,根据数据长度 ...

  4. jenkins 新增用户和修改用户名密码

    在某些条件下,jenkins是不允许注册用户的,这是,你可以采用如下的方式来新增用户,对于老的用户,忘记密码了,使用如下方式来重置密码. 1.系统管理-->管理用户 ----> 新建用户 ...

  5. [Java in NetBeans] Lesson 05. Method/function

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Define a method:(motivation: write one time, but use it many times ...

  6. Adobe Acrobat 9 Pro序列号

    其实只删除c:\Program Files\Common Files\Adobe\Adobe PCD\cache目录下的cache.db文件也是可以的,然后重新打开Adobe ,输入序列号1118-4 ...

  7. Springboot整合Mybatis 之分页插件使用

    1: 引入jar包 <!-- 引入MyBatis分页插件--> <dependency> <groupId>com.github.pagehelper</gr ...

  8. ECC

    素数 prime,又称为质数,是指,除了1和它本身,没有其他因数的数. 素数的定理: 1)在一个大于1的数a和它的2倍之间必定存在至少一个素数: 素数的性质: 1)在所有的大于10的质数中,个位数,只 ...

  9. Python基础知识摘要

    python字典 增,删,改,查 1.增:XXX[新的key] = value 2.删:DEL XXX[key] 3.改:XXX[已经存在的key] = NewValue 4.查:aList.exte ...

  10. hdu4778 状态压缩

    #include <iostream> #include <algorithm> #include <cstdio> #include <vector> ...