Source:

PAT A1020 Tree Traversals (25 分)

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的更多相关文章

  1. Tree Traversals

    Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...

  2. HDU 1710 二叉树的遍历 Binary Tree Traversals

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. hdu1710(Binary Tree Traversals)(二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. HDU1710Binary Tree Traversals

    HDU1710Binary Tree Traversals 题目大意:给一个树的前序遍历和中序遍历,要求输出后序遍历. (半年前做这道题做了两天没看懂,今天学了二叉树,回来AC了^ ^) 首先介绍一下 ...

  5. HDU-1701 Binary Tree Traversals

    http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Tr ...

  6. 03-树2. Tree Traversals Again (25)

    03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  7. HDU 1710-Binary Tree Traversals(二进制重建)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  8. PAT1086:Tree Traversals Again

    1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  9. Binary Tree Traversals(HDU1710)二叉树的简单应用

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. java EE学习流程(第二版更新)

    这周有点堕落了,这两天啥都没写,就顾上刷<庆余年>了

  2. NOIp2018集训test-10-23

    上午考了一套sb题,但是没有人AK.李巨290虐场. 下午又考了一套sb题,李巨AK虐场.%%% T1 % 中国剩余定理好像做不了啊,我一直在想如何用CRT做,然后就GG了. 然而正解是bike当初说 ...

  3. (转)openfire插件开发(三)通过http方式向openfire客户端发信息

    转:http://blog.csdn.net/hzaccp3/article/details/19964655 需求:  通过http方式,向openfire客户端发信息(非XMPP协议)openfi ...

  4. goroutine 需要注意的一个小细节

    虽然goroutine 是并发执行的,但是它们并不是并行运行的.如果不告诉Go 额外的东西,同一时刻只会有一个goroutine 执行.利用runtime.GOMAXPROCS(n) 可以设置goro ...

  5. B606 ChangeNet

    @echo off Setlocal Enabledelayedexpansion title B606 ChangeNet echo Checking... set inside=F&set ...

  6. TLS/SSL 协议 - Server Certificate

    Server Certificate 典型的Certificate消息用于携带服务器X.509证书链.证书链是以ASN.1 DER编码的一系列证书,一个接着一个组合而成.主证书必须第一个发送,中间证书 ...

  7. js读取json数据

    { "code": 0, "msg": null, "data": { "pageNum": 1, "page ...

  8. ThinkPHP5实用的数据库操作方法

    1.update方法总结 /** * 设置记录的某个字段值 * 支持使用数据库字段和方法 * @access public * @param string|array $field 字段名 * @pa ...

  9. c# winForm DotNetBar控件之SuperGridControl

    1.添加表头 sgc.PrimaryGrid.SelectionGranularity = SelectionGranularity.Row;//点击选中一行 DevComponents.DotNet ...

  10. 洛谷 P2652 同花顺(离散化)

    洛谷 P2652 同花顺(题面) 手动模拟了一下,其实离散化排序可以起很大作用题目要求花色相同,数字连续,那么我们要做的就是找一种花色,并提取出其中一串数字留下那些舍弃的牌换成相应花色,并和之前留下的 ...