【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 ...
随机推荐
- Luogu4770 NOI2018你的名字(后缀数组+线段树)
即求b串有多少个本质不同的非空子串,在a串的给定区间内未出现.即使已经8102年并且马上就9102年了,还是要高举SA伟大旗帜不动摇. 考虑离线,将所有询问串及一开始给的串加分隔符连起来,求出SA.对 ...
- LOJ2542 PKUWC2018随机游走(概率期望+容斥原理)
如果直接dp,状态里肯定要带上已走过的点的集合,感觉上不太好做. 考虑一种对期望的minmax容斥:其中Max(S)为遍历完S集合的期望步数,Min(S)为遍历到S集合中一个点的期望步数.当然才不管怎 ...
- Vitrualbox 桥接网卡界面名称未指定、Filters currently installed on the system have reached the limit、不能为虚拟电脑 打开一个新任务
1. 桥接网卡界面名称未指定 http://wenku.baidu.com/link?url=VFG0hknsDX3VPXQoX5f-g1wUX_LBl-lOj0ZqD222kM31iVCPJKVu3 ...
- 洛谷 P3629 [APIO2010]巡逻 解题报告
P3629 [APIO2010]巡逻 题目描述 在一个地区中有 n 个村庄,编号为 1, 2, ..., n.有 n – 1 条道路连接着这些村 庄,每条道路刚好连接两个村庄,从任何一个村庄,都可以通 ...
- debian开启cgroup memory子系统
Debian系统默认是关闭cgroup memory子系统的. 1.修改grub启动参数 # vim /etc/default/grub GRUB_CMDLINE_LINUX="cgroup ...
- spring的controller默认是单例还是多例
转: spring的controller默认是单例还是多例 先看看spring的bean作用域有几种,分别有啥不同. spring bean作用域有以下5个: singleton:单例模式,当spri ...
- laravel 命令行测试 Uncaught ReflectionException: Class config does not exist
require __DIR__ . '/vendor/autoload.php'; $app = require_once __DIR__ . '/bootstrap/app.php'; config ...
- python数据分析美国大选项目实战(三)
项目介绍 项目地址:https://www.kaggle.com/fivethirtyeight/2016-election-polls 包含了2015年11月至2016年11月期间对于2016美国大 ...
- create-react-app脚手架使用
1.安装脚手架和路由 npm i -g create-react-app npm i -S react-router react-router-dom 2.创建新项目 create-react-app ...
- HttpDebug下载
话不多说,早就有了这个,有一天公司地址有限制,网盘访问不了,看见很多博客园上的下载都需要积分,忍不了就发出来共享吧! HttpDebug下载: https://files.cnblogs.com/fi ...