PAT Advance 1020
题目:
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<queue>
using namespace std;
typedef struct _Node
{
int num;
struct _Node *lchild;
struct _Node *rchild;
}Node, *PNode;
int t1[1001],t2[1001];
int n;
int getPosition(int a){
int i;
for(i=1;i<=n;i++)
if(a == t2[i])
return i;
}
void createTree(PNode &node, int i, int j, int len){
if(len<=0){
return ;
}
node = new Node;
node->num = t1[i];
node->lchild = NULL;
node->rchild = NULL;
int m = getPosition(t1[i]);
createTree(node->lchild,i-len+m-j,j,m-j);//m-j:左子树len,len-1-(m-j):右子树len
createTree(node->rchild,i-1,m+1,len-1-m+j);
}
void PreTravelTree(PNode pn) //前序递归遍历
{
if(pn){
cout<<pn->num<<" "<<endl;
PreTravelTree(pn->lchild);
PreTravelTree(pn->rchild);
}
}
int main()
{
int i;
int r[100];
queue<PNode> q;
cin>>n;
for(i=1;i<=n;i++)
cin>>t1[i];
for(i=1;i<=n;i++)
cin>>t2[i];
PNode root = NULL,temp;
createTree(root,n,1,n);
q.push(root);
i=0;
while(!q.empty()){
temp = q.front();
q.pop();
r[i] = temp->num;
i++;
if(temp->lchild!=NULL)
q.push(temp->lchild);
if(temp->rchild!=NULL)
q.push(temp->rchild);
}
cout<<r[0];
for(i=1;i<n;i++)
cout<<" "<<r[i];
cout<<endl;
return 0;
}
PAT Advance 1020的更多相关文章
- PAT(B) 1020 月饼(Java)
题目链接:1020 月饼 (25 point(s)) 分析 将月饼(库存量,总售价,单价)封装成MoonCake类 Scanner会超时,用BufferedReader类读取数据 读取的时候用字符串数 ...
- PAT乙级 1020. 月饼 (25)(只得到23分)
1020. 月饼 (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 月饼是中国人在中秋佳节时吃的一种传统食 ...
- PAT Basic 1020
1020 月饼 (25 分) 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意 ...
- PAT 乙级 1020 月饼 (25) C++版
1020. 月饼 (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 月饼是中国人在中秋佳节时吃的一种传统食 ...
- 【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乙级1020
1020 月饼 (25 分) 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- pat甲级1020中序后序求层序
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 intege ...
随机推荐
- CodeForces 390E Inna and Large Sweet Matrix(树状数组改段求段)
树状数组仅仅能实现线段树区间改动和区间查询的功能,能够取代不须要lazy tag的线段树.且代码量和常数较小 首先定义一个数组 int c[N]; 并清空 memset(c, 0, sizeof c) ...
- Redis使用经验
首先,缓存的对象有三种: 1:数据库中单条的的数据(以表名跟id作为key永久保存到Redis),在有更新的地方都要更新缓存(不适用于需要经常更新的数据): 2:对于一些不分页,不需要实时(需要多表查 ...
- hibernate 多对多单向
多对多会利用中间表做外键的形式关联 package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persi ...
- oracle 事务多表查询以及额外的用处
/* 以下代码是对emp表进行显示宽度设置*/ col empno for 9999;col ename for a10;col job for a10;col mgr for 9999;col hi ...
- ajax 参数data问题 data中的 参数名 参数值为string 提交到后台后,会自动转换参数名相同的 类型 和 js字符串拼接
latlng"14.6005238,100.43635419999998"Cusid"accb5c1b-6aef-4f3b-a4eb-d60ea1ca5f54" ...
- PowerShell---Operators 介绍
1.Arithmetic operators(算术运算符) 算术运算符包括加.减.乘.除.取模 此外,加法运算符 (+) 和乘法运算符 (*) 还可对字符串.数组和哈希表进行运算.加法运算符将输入连接 ...
- log4j使用示例
### set log levels ### log4j.rootLogger = INFO , D #INFO , C , D , E ### console ### #log4j.appender ...
- Atitit.二维码功能的设计实践 attilax 总结
Atitit.二维码功能的设计实践 attilax 总结 1.1. 二维码要实现的功能1 1.2. 现有二维码功能设计不足的地方(待改进)1 1.3. 二维码组件1 1.4. Java版 zxing ...
- java使用lambda表达式对List集合进行操作(JDK1.8)
1. 使用lambda表达式对List集合进行操作(JDK1.) List<TreeDto> collect = list.stream().filter(item -> item. ...
- firefox配置
Firefox23取消了一个很人性化的功能,就是在GUI界面中禁用JavaScript,对于这点我很不能理解!JavaScript是所有网页木马 的源头,防人之心不可无,FireFox就这么确信能防住 ...