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 ...
随机推荐
- atitit.新增编辑功能 跟orm的实现 attilax p31
atitit.新增编辑功能 跟orm的实现 attilax p31 1. 流程的实现 1 2. view的实现(dwr) 1 3. 获取表结构 1 4. grep filt req params 2 ...
- vim -d file01 file02 diff file01 file02 对比两文件的不同
[root@86 vhosts]# vim -d defaul.conf.bak zabbix.xinxianm.com.conf server { | server { listen 80; | l ...
- EXTI—外部中断/事件控制器
外部中断/事件控制器(EXTI)管理了控制器的 23 个中断/事件线.每个中断/事件线都对应有一个边沿检测器,可以实现输入信号的上升沿检测和下降沿的检测. EXTI 可以实现对每个中断/事件线进行单独 ...
- centos IPTables 配置方法
entos IPTables 配置方法 http://os.51cto.com/art/201103/249359_1.htm iptables 指南 1.1.19 http://www.frozen ...
- Android——gridLayout(网格布局)
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android=" ...
- SSL/TLS协议运行机制的概述_转
转自:SSL/TLS协议运行机制的概述 作者: 阮一峰 日期: 2014年2月 5日 互联网的通信安全,建立在SSL/TLS协议之上. 本文简要介绍SSL/TLS协议的运行机制.文章的重点是设计思想和 ...
- 深入理解JVM--JVM垃圾回收机制(转)
Java语言出来之前,大家都在拼命的写C或者C++的程序,而此时存在一个很大的矛盾,C++等语言创建对象要不断的去开辟空间,不用的时候有需要不断的去释放控件,既要写构造函数,又要写析构函数,很多时候都 ...
- 树链剖分 + 后缀数组 - E. Misha and LCP on Tree
E. Misha and LCP on Tree Problem's Link Mean: 给出一棵树,每个结点上有一个字母.每个询问给出两个路径,问这两个路径的串的最长公共前缀. analyse: ...
- 数学分析 + 容斥原理 - URAL 1907 Coffee and Buns
Coffee and Buns Problem's Link: http://www.bnuoj.com/v3/contest_show.php?cid=6415#problem/H Mean: 给定 ...
- 文件 I/O 问题
文件 I/O 问题:(1)对不存在的或者错误的文件进行操作吗?(2)文件以不正确的方式打开吗?(3)文件结束判断不正确吗?(4)没有正确地关闭文件吗? #include <iostream> ...