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 ...
随机推荐
- python的代码块缓存机制,小数据池机制。
同一代码块的缓存机制 在python中一个模块,一个函数,一个类,一个文件等都是一个代码块. 机制内容:Python在执行同一个代码块的初始化对象的命令时,会检查是否其值是否已经存在,如果存在,会将其 ...
- jmeter登录之-动态参数
jmeter登录之-动态参数 1.抓包查看提交的登录参数 发现参数authenticity_token是动态的,每次都不一样,所以回放的时候就会失败 2.提取动态变化的参数-后置处理器(相当于LR的关 ...
- VC内联汇编,引用程序中的变量
int a=5; //变量a _asm { mov eax,a; //将变量a的值放入寄存器eax add eax,eax; //相当于a=a+a mov a,eax; // ...
- elasticsearch5.5.2安装
elasticsearch5.x安装中一些问题的解决办法 最近在学习elk,由于编译安装使用5.2.1版本的elasticsearch,所以遇到了很多问题,下面是一些问题及解决办法. 1.修改访问e ...
- Ubuntu管理员密码设置
最近学习嵌入式编程,首先准备搭建一个嵌入式开发环境. 由于想省钱,就准备搭建一个虚拟的arm系统用于测试学习. 虚拟系统搭建与linux系统上,暂定使用Ubuntu+qemu进行环境搭建. 在进行Ub ...
- docker集群故障迁移
docker swarm 故障时候镜像迁移(无法添加新节点的时候)生产docker集群出现了故障,无法正常添加删除节点.在这样的情况下只能想办法把故障集群的镜像迁移到新的docker集群当中.将发生故 ...
- 实现Comparator接口和Comparable接口,以及Map按value排序 ,map遍历
继承Comparator接口,重写compare()方法 import java.util.ArrayList; import java.util.Arrays; import java.util.C ...
- c#委托(Delegates)--基本概念及使用 转发
在我这菜鸟理解上,委托就是可以用方法名调用另一方法的便捷方法,可以简化switch等语句的重复.最近做项目的时候恰好需要用到委托,便来复习及学习委托的使用.嗯...本人以前并没有用过,只是稍微知道而已 ...
- 2.4 webpack + gulp 构建完整前端工作流
在前面的两个小节中已经完整的讲了 webpack 和 gulp 相关的内容,本小节中将会结合二者构建一个完整的前端工作流,内容目录为: 前端工程结构和目标 前端工程目录结构 gulp clean gu ...
- 1085 Perfect Sequence (25 分)
Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...