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 ...
随机推荐
- 【LeetCode 2】两数相加
描述 [题解] 模拟高精度的加法. 用x来记录前面的进位就好. [代码] /** * Definition for singly-linked list. * struct ListNode { * ...
- Service6
rsync同步操作 同步 : 只传输变化的数据 复制:完整的传输 • 命令用法– rsync [选项...] 源目录 目标目录 • 同步与复制的差异– 复制:完全拷贝源到目标– 同步:增量拷贝 ...
- DOM学习总结(三)DOM访问/操作
DOM访问理解:找到这个标签元素,然后才能对它进行操作 1.getElementById() 方法document.getElementById(""); //通过id名字来找到 ...
- Hbase节点的管理|服役和退役节点
Base节点的管理 1.服役(commissioning) 当启动regionserver时,regionserver会向Hmaster注册并开始接收本地数据,开始的时候,新加入的节点不会有任何数据, ...
- BZOJ 3430: [Usaco2014 Jan]Ski Course Rating(并查集+贪心)
题面 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 136 Solved: 90 [Submit][Status][Discuss] Descript ...
- 2.4 webpack + gulp 构建完整前端工作流
在前面的两个小节中已经完整的讲了 webpack 和 gulp 相关的内容,本小节中将会结合二者构建一个完整的前端工作流,内容目录为: 前端工程结构和目标 前端工程目录结构 gulp clean gu ...
- 6. Python运算符之算术、比较、赋值运算符
什么是运算符?举个简单的例子 4 +1 = 5 . 例子中,4 和 1 被称为操作数,"+" 和"="称为运算符. 工作中用到的运算符主要有以下几种:算术运算符 ...
- 通过Module读取寄存器的值
1: int eax; 2: _asm_("nop":"=a"(eax)); 3: printk("Get Eax Value:\n"); ...
- Collection Lists
ArrayList LinkedList Vector 顺序添加 抽象数据类型(ADT)是一个实现包括储存数据元素的存储结构以及实现基本操作的算法. ArrayList (1)ArrayList是 ...
- 拾遗:Git 与 Svn hook 不执行问题
要点: GIT 或 SVN 的 hook 执行之前,会将所有环境变量清空,因此在其中执行命令时,必须指定绝对路径或重新设置必要的环境变量,如:$HOME 等 修改为正确的名称,如:post-commi ...