【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 ...
随机推荐
- 【题解】 bzoj1503: [NOI2004]郁闷的出纳员 (Splay)
bzoj1503,懒得复制,戳我戳我 Solution: 我知不知道我是那根筋抽了突然来做splay,调了起码\(3h+\),到第二天才改出来(我好菜啊),当做训练调错吧 一个裸的splay,没啥好说 ...
- 如何使用Python对Instagram进行数据分析?
我写此文的目的在于展示以编程的方式使用Instagram的基本方法.我的方法可用于数据分析.计算机视觉以及任何你所能想到的酷炫项目中.Instagram是最大的图片分享社交媒体平台,每月活跃用户约五 ...
- 解题:CF1063F String Journey
题面 分析性质以进行DP 性质1:一定有一个最优解通过每次删除第一个或最后一个字符达到 这个脑补一下就能证明了 那么我们设$dp[i]$表示后缀$[i,n]$选出一个前缀所能达到的最大长度,从右往左D ...
- SPOJ6340 ZUMA - ZUMA
题意:n个珠子排成一排,都有各自的颜色. 你可以选择不少于w个连续同色的珠子消掉,也可以先放着.你还可以任意插入任意颜色的珠子. 求全部消掉至少要插入几个珠子. 解: 什么毒瘤东西...... 有个十 ...
- 【CF580C】Kefa and Park
题目大意:给定一棵 N 个节点的有根树(其中根节点始终为 1 号节点),点有点权,点权只有 1 和 0 两种,求从根节点到叶子节点的路径中,有多少条路径满足:路径上最大连续点权为 1 的节点个数不超过 ...
- linux c 编程 ------ 串口编程
http://blog.csdn.net/specialshoot/article/details/50707965 对于串口的打开操作,必须使用O_NOCTTY参数.O_NOCTTY如果路径名指向终 ...
- RabbitMQ的安装部署
RabbitMQ安装部署 一.软件准备 wget http://erlang.org/download/otp_src_19.3.tar.gz wget http://www.rabbitmq.com ...
- 「Vue」JS方法学习
1.构造函数 大写开头的,能被NEW一个新实例,实例即执行回调函数 异步返回数据.then指定回调函数的时候,成功的回调函数必须传,失败的回调可以不传 var fs = require('fs') f ...
- P2073 送花
P2073 送花 题目背景 小明准备给小红送一束花,以表达他对小红的爱意.他在花店看中了一些花,准备用它们包成花束. 题目描述 这些花都很漂亮,每朵花有一个美丽值W,价格为C. 小明一开始有一个空的花 ...
- bzoj千题计划226:bzoj2763: [JLOI2011]飞行路线
http://www.lydsy.com/JudgeOnline/problem.php?id=2763 这也算分层图最短路? dp[i][j]到城市i,还剩k次免费次数的最短路 #include&l ...