【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(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 (<=30), 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
C++代码如下:
#include<iostream>
#include<queue>
using namespace std; struct node {
int data;
node *lchild, *rchild;
}; int post[], in[];
int n; node* create(int posL,int posR,int inL,int inR) {
if (posL>posR) return NULL;
node *root = new node;
root->data = post[posR];
int k;
for (k = inL; k <=inR; k++) {
if (in[k] == post[posR]) break;
}
int numL = k-inL;
root->lchild = create(posL, posL + numL-, inL, k - );
root->rchild = create(posL + numL, posR - , k + , inR); return root;
} int num = ; void level(node*r) {
if (r == NULL) return;
queue<node*>q;
q.push(r);
while (!q.empty()) {
node *top = q.front();
q.pop();
num++;
cout << top->data;
if (num < n) cout << ' ';
if (top->lchild != NULL) q.push(top->lchild);
if (top->rchild != NULL) q.push(top->rchild);
}
} int main() {
cin >> n;
int t;
for (int i = ; i < n; i++) {
cin >> t;
post[i] = t;
}
for (int i = ; i < n; i++) {
cin >> t;
in[i] = t;
}
node*root = create(, n - , , n - );
level(root);
return ;
}
【PAT】1020 Tree Traversals (25)(25 分)的更多相关文章
- PAT 1020. Tree Traversals
PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT A1020 Tree Traversals (25 分)——建树,层序遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020 Tree Traversals (25 分)(二叉树的遍历)
给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历 #include<bits/stdc++.h> using namespace std; ; int in[N]; int pos ...
- PAT A1020 Tree Traversals(25)
题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
随机推荐
- MySQL的group_concat()函数合并多行数据
一个很有用的函数 group_concat(),手册上说明:该函数返回带有来自一个组的连接的非NULL值的字符串结果. 通俗点理解,其实是这样的:group_concat()会计算哪些行属于同一组,将 ...
- MT【183】借力打力
(2011安徽省赛)设$f(x)=ax^3+bx+c(a,b,c\in R)$,当$0\le x \le1$时,$0\le f(x)\le1$,求$b$的可能的最大值. 分析:$f(0)=c,f(1) ...
- 洛谷 P4070 [SDOI2016]生成魔咒 解题报告
P4070 [SDOI2016]生成魔咒 题目描述 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 \(1\).\(2\) 拼凑起来形成一个魔咒串 \([1,2]\). 一个魔咒 ...
- bzoj1178/luogu3626 会议中心 (倍增+STL::set)
贪心地,可以建出一棵树,每个区间对应一个点,它的父亲是它右边的.与它不相交的.右端点最小的区间. 为了方便,再加入一个[0,0]区间 于是就可以倍增来做出从某个区间开始,一直到某个右界,这之中最多能选 ...
- bzoj1345 序列问题 (贪心)
考虑某个点产生的贡献: 如果i左边是一个比它小的数x,那有两种情况: 1.x的左边的数y大于i,肯定要把x合并到i,i的贡献++ 2.x的左边的数y小于i,那肯定要把x合并到y,而这时候递归地来考虑, ...
- POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路径)
POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom ...
- [IOI2018] werewolf 狼人
[IOI2018] werewolf 狼人 IOI2018题解 (其实原题强制在线,要用主席树) 代码: 注意: 1.下标从0~n-1 2.kruskal重构树开始有n个节点,tot从n开始,++to ...
- 卸载并安装指定版本Angular CLI
1.卸载之前的版本 npm uninstall -g @angular/cli 2.清除缓存,确保卸载干净 npm cache clean 3.检查是否卸载干净 输入命令 ng -v 若显示comma ...
- Mysql select id 加上order by 后结果不一致
测试数据将近280万 1.SELECT id FROM cbbd ORDER BY id LIMIT 900000,10 2.SELECT id FROM cbbd LIMIT 900000,10 ...
- [原]Android开发优化-Adapter优化
ListView作为Android开发中使用频率最高的一个控件,保证ListView的流畅运行,对用户体验的提高至关重要.Adapter是ListView和数据源之间的中间人,当每条数据进入可见区时, ...