PAT 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 <stdio.h>
#include <stdlib.h> typedef struct Node{
int data;
struct Node *left,*right;
}Node;
int postOrder[];
int inOrder[];
Node* buildTree(int,int,int,int);
void printAndDestroyTree(Node *);
int main()
{
int N,i;
while(scanf("%d",&N) != EOF){
for(i=;i<N;++i){
scanf("%d",&postOrder[i]);
}
for(i=;i<N;++i){
scanf("%d",&inOrder[i]);
}
Node *tree = buildTree(,N-,,N-);
printAndDestroyTree(tree);
}
return ;
} Node* buildTree(int postStart,int postEnd,int inStart,int inEnd)
{
if(postStart == postEnd){
Node *p = (Node *)malloc(sizeof(Node));
p->data = postOrder[postStart];
p->left = p->right = NULL;
return p;
}
else{
Node *p = (Node *)malloc(sizeof(Node));
p->data = postOrder[postEnd];
p->left = p->right = NULL;
int i = inStart;
while(i<=inEnd && postOrder[postEnd] != inOrder[i++]);
if(i > inStart+)
p->left = buildTree(postStart,postStart+i-inStart-,inStart,i-);
if(i <= inEnd)
p->right = buildTree(postStart+i-inStart-,postEnd-,i,inEnd);
return p;
}
} void printAndDestroyTree(Node *p)
{
if(!p)
return;
Node *nodeArray[];
int top=,base=;
Node *q;
nodeArray[top++] = p;
while(top>base){
q = nodeArray[base++];
if(base == )
printf("%d",q->data);
else
printf(" %d",q->data);
if(q->left)
nodeArray[top++] = q->left;
if(q->right)
nodeArray[top++] = q->right;
free(q);
}
printf("\n");
}
PAT 1020的更多相关文章
- PAT 1020. Tree Traversals
PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 1020 月饼 (25)(精简版代码+思路+推荐测试用例)
1020 月饼 (25)(25 分)提问 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是 ...
- PAT——1020. 月饼
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...
- PAT 1020. 月饼 (25)
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...
- PAT 1020 月饼
https://pintia.cn/problem-sets/994805260223102976/problems/994805301562163200 月饼是中国人在中秋佳节时吃的一种传统食品,不 ...
- PAT 1020月饼
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...
- PAT 甲级 树专题小结
1.已知两个序链表建树 先序中序建树 PAT 1086 node *buildTree(vector<int>pre,vector<int>in,int pl,int pr,i ...
- PAT(B) 1020 月饼(Java)
题目链接:1020 月饼 (25 point(s)) 分析 将月饼(库存量,总售价,单价)封装成MoonCake类 Scanner会超时,用BufferedReader类读取数据 读取的时候用字符串数 ...
随机推荐
- Canvas入门(2):图形渐变和图像形变换
来源:http://www.ido321.com/986.html 一.图形渐变(均在最新版Google中测试) 1.绘制线性渐变 1: // 获取canvas 的ID 2: var canvas = ...
- CSS基础(背景、文本、列表、表格、轮廓)
CSS 背景属性 属性 描述 background 简写属性,作用是将背景属性设置在一个声明中. background-attachment 背景图像是否固定或者随着页面的其余部分滚动. backgr ...
- Spring学习笔记(二)Spring基础AOP、IOC
Spring AOP 1. 代理模式 1.1. 静态代理 程序中经常需要为某些动作或事件作下记录,以便在事后检测或作为排错的依据,先看一个简单的例子: import java.util.logging ...
- Linux下搭建jdk
1.上oracle下载jdk-8u73-linux-x64.gz文件(其他版本的也可以,不过本教程以.gz格式为准,其他格式没有尝试过,不造怎么弄) 2.在linux桌面上右键->打开终端 3. ...
- mysql 错误解决
1. Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE ...
- Adress
流水账的写法:因为不想让亲爱的你只看开头就关掉了我辛苦的劳作.流水账的好处是:便于逻辑的理解 http://software.intel.com/zh-cn/blogs/2014/01/20/cent ...
- Nginx的session一致性问题
session一致性memcached缓存数据库解决方案 1.安装memcached内存数据库 yum –y install memcached 可以用telnet localhost 11211 S ...
- homework-10
不多不说这是一次神奇的作业,作业一拖再拖,到最后发现.... 首先,在一开始的最大字数和问题实现图形界面主要是由我的小伙伴邹同学完成的,所以当我第一次看到说要显示详细运行过程的时候感到很迷茫. 第一感 ...
- JNI调用测试
有需求使用JNI调用,籍着这个机会按照<Linux下测试Java的JNI(Java Native Interface)>上进行了下测试. 这篇文章记录得很清楚了,对原理未做深入的分析,希望 ...
- UVALive 7281 Saint John Festival (凸包+O(logn)判断点在凸多边形内)
Saint John Festival 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/J Description Porto's ...