PAT_A1020#Tree Traversals
Source:
Description:
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
Keys:
Code:
/*
time: 2019-06-30 14:40:45
problem: PAT_A1020#Tree Traversals
AC: 08:33 题目大意:
给出后序和中序遍历,打印层序遍历
*/
#include<cstdio>
#include<queue>
using namespace std;
const int M=;
int post[M],in[M],n;
struct node
{
int data;
node *lchild,*rchild;
}; node *Create(int postL, int postR, int inL, int inR)
{
if(postL > postR)
return NULL;
node *root = new node;
root->data = post[postR];
int k;
for(k=inL; k<=inR; k++)
if(in[k] == post[postR])
break;
int numLeft = k-inL;
root->lchild = Create(postL,postL+numLeft-,inL,k-);
root->rchild = Create(postL+numLeft,postR-,k+,inR);
return root;
} void LayerOrder(node *root)
{
queue<node*> q;
q.push(root);
int pt=;
while(!q.empty())
{
root = q.front();
q.pop();
printf("%d%c", root->data, ++pt==n?'\n':' ');
if(root->lchild)
q.push(root->lchild);
if(root->rchild)
q.push(root->rchild);
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d", &n);
for(int i=; i<n; i++)
scanf("%d", &post[i]);
for(int i=; i<n; i++)
scanf("%d", &in[i]);
node *root = Create(,n-,,n-);
LayerOrder(root); return ;
}
PAT_A1020#Tree Traversals的更多相关文章
- Tree Traversals
Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...
- HDU 1710 二叉树的遍历 Binary Tree Traversals
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU1710Binary Tree Traversals
HDU1710Binary Tree Traversals 题目大意:给一个树的前序遍历和中序遍历,要求输出后序遍历. (半年前做这道题做了两天没看懂,今天学了二叉树,回来AC了^ ^) 首先介绍一下 ...
- HDU-1701 Binary Tree Traversals
http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Tr ...
- 03-树2. Tree Traversals Again (25)
03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- HDU 1710-Binary Tree Traversals(二进制重建)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- PAT1086:Tree Traversals Again
1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- Binary Tree Traversals(HDU1710)二叉树的简单应用
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- P1064 金明的预算方案 (分组背包稍稍变形)
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱就行”. ...
- [7.18NOIP模拟测试5]星际旅行 题解
题面(加密) 考场上靠打表yy出的规律进而想到的正解233333 可以把一条双向边拆成两条单向边,这样的话每个点度数都为偶数,符合欧拉图的定义. 那么题目可以转化为:去掉两条边,使图中存在一条欧拉路. ...
- js委托事件-addEventListeners(冒泡方向)
JQuery中live().delegate().on()事件都能给新增元素绑定事件,原理就是用了事件委托. 实例: 给id为div的元素绑定一个click委托,如果冒泡上来的元素是P元素就会执行al ...
- robotframework + selenium2library 一点测试的经验
1 对于元素的外层包括frame/iframe标签的.一定要先select frame name=xxx,然后再操作元素. Select frame name=新建个案 click element ...
- DOS和DDOS攻击
1.什么是DOS攻击 首先注意这里是DOS(Denial Of Service)攻击,并非DoS.DoS是微软早期的系统版本. DoS即Denial Of Service,拒绝服务的缩写.DoS是指故 ...
- flask json
导入 from flask import Flask,jsonify 1.列表 def index(): arr=['mkdir','md','touch'] return jsonify(arr) ...
- The Preliminary Contest for ICPC Asia Nanjing 2019( B H F)
B. super_log 题意:研究一下就是求幂塔函数 %m的值. 思路:扩展欧拉降幂. AC代码: #include<bits/stdc++.h> using namespace std ...
- POJ 1673 EXOCENTER OF A TRIANGLE(解三角形重心)
题目链接:http://poj.org/problem?id=1673 AC代码: #include<cstdio> #include<cmath> #include<a ...
- JUC源码分析-集合篇(八)DelayQueue
JUC源码分析-集合篇(八)DelayQueue DelayQueue 是一个支持延时获取元素的无界阻塞队列.队列使用 PriorityQueue 来实现. 队列中的元素必须实现 Delayed 接口 ...
- nginx基本配置各个参数说明
#运行用户user nobody;#启动进程,通常设置成和cpu的数量相等worker_processes 1; #全局错误日志及PID文件#error_log logs/error.log;#err ...