PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (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
根据后续遍历和中序遍历,求二叉树
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <queue> using namespace std;
typedef struct Tree
{
int data;
Tree *lchild;
Tree *rchild;
}a[40];
int post[40];
int in[40];
int n;
int ans[40];
void dfs(int l1,int r1,int l2,int r2,Tree* &root)
{
root=new Tree();
int i;
for( i=l1;i<=r1;i++)
if(in[i]==post[r2])
break;
root->data=post[r2];
if(i==l1)
root->lchild=NULL;
else
dfs(l1,i-1,l2,l2+i-l1-1,root->lchild);
if(i==r1)
root->rchild=NULL;
else
dfs(i+1,r1,r2-(r1-i),r2-1,root->rchild);
}
int cnt;
void bfs(Tree *tree)
{
queue<Tree*> q;
q.push(tree);
while(!q.empty())
{
Tree *root=q.front();
q.pop();
ans[cnt++]=root->data;
if(root->lchild!=NULL)
q.push(root->lchild);
if(root->rchild!=NULL)
q.push(root->rchild);
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&post[i]);
for(int i=1;i<=n;i++)
scanf("%d",&in[i]);
Tree *tree;
dfs(1,n,1,n,tree);
cnt=0;
bfs(tree);
for(int i=0;i<cnt;i++)
{
if(i==cnt-1)
printf("%d\n",ans[i]);
else
printf("%d ",ans[i]);
}
}
return 0;
}
PAT 甲级 1020 Tree Traversals (二叉树遍历)的更多相关文章
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
随机推荐
- XMPP 基础 (转载)
技术博客 http://www.cnblogs.com/ChenYilong/ 新浪微博 http://weibo.com/luohanchenyilong Fullscreen XMPP 基础 ...
- c语言优先级和结合性
C语言的运算符众多,具有不同的优先级和结合性,我们将它们全部列了出来,方便大家对比和记忆: 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右 ...
- 使用libjpeg 压缩yuv420到jpg (内存方式)
#include <Windows.h> #include <stdio.h> extern "C" { #include <jpeglib.h> ...
- python用time函数计算程序运行时间
内置模块time包含很多与时间相关函数.我们可通过它获得当前的时间和格式化时间输出. time(),以浮点形式返回自Linux新世纪以来经过的秒数.在linux中,00:00:00 UTC, Janu ...
- 高德地图API INVALID_USER_SCODE问题以及keystore问题
今天这篇文章会给大家介绍三个问题: 1,接入API时出现invalid_user_scode问题 首先进行第一个大问题,接入高德地图API时出现invalid_user_scode问题 因为项目需要接 ...
- SQL查询优化联合索引 与 单一列的索引
目前WEB的普及太快,在实际的开发中,一旦遇到大数据量的时候就需要做到优化,让查询的更快,才能给客户更好的体验,也能够在程序上避免timeout. 部分转载自:https://www.cnblogs. ...
- Can't zip RDDs with unequal numbers of partitions
java.lang.IllegalArgumentException: Can't zip RDDs with unequal numbers of partitions //如果两个RDD分区数不同 ...
- ipmitool sdr type Temperature sdr 从传感器获取某一类数据
1.1.1 常用监控命令总结.ipmitool sdr type Temperature 硬件监控 yum install OpenIPMI ipmitool ipmitool sdr type T ...
- IDEA 中 右键新建时,没有新建class的解决方案
今天在用IDEA新建包后,因为我新建的包名是userprivman-data这样的包,然后导致右键后没法新建类,并且在新建包名的时候就提示:不合法的包名,可能不会新建类 然后右键单击包名,发现没有新建 ...
- 梯度下降算法到logistic回归
http://sbp810050504.blog.51cto.com/2799422/1608064/ http://blog.csdn.net/dongtingzhizi/article/detai ...