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的更多相关文章

  1. PAT 1020. Tree Traversals

    PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...

  2. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  3. PAT 1020 月饼 (25)(精简版代码+思路+推荐测试用例)

    1020 月饼 (25)(25 分)提问 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是 ...

  4. PAT——1020. 月饼

    月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...

  5. PAT 1020. 月饼 (25)

    月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...

  6. PAT 1020 月饼

    https://pintia.cn/problem-sets/994805260223102976/problems/994805301562163200 月饼是中国人在中秋佳节时吃的一种传统食品,不 ...

  7. PAT 1020月饼

    月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...

  8. PAT 甲级 树专题小结

    1.已知两个序链表建树 先序中序建树 PAT 1086 node *buildTree(vector<int>pre,vector<int>in,int pl,int pr,i ...

  9. PAT(B) 1020 月饼(Java)

    题目链接:1020 月饼 (25 point(s)) 分析 将月饼(库存量,总售价,单价)封装成MoonCake类 Scanner会超时,用BufferedReader类读取数据 读取的时候用字符串数 ...

随机推荐

  1. IOS AsyncSocket

    导入AsyncSocket.h  AsyncSocket.m   AsyncUdpSocket.h   AsyncUdpSocket.m   以及  CFNetWork.framework async ...

  2. C#实现对文件目录的实时监控

    本文主要描述如何通过C#实现实时监控文件目录下的变化,包括文件和目录的添加,删除,修改和重命名等操作. 首先,我们需要对.net提供的FileSystemWatcher类有所了解.我有些懒,找了MSD ...

  3. Away 3D 之 交互和渐变----Interactivity and Tweening

    在这个教程中,你将学会如何创建一个地板对象,本教程中的地板是可交互的并且能够移动小方块到鼠标的点击的地方. 1. 设置场景: 你正在创建的场景包含了一个平面,地板和一个看起来像一个饰品的方块,还有一个 ...

  4. 第四章:更多的bash shell命令

    第四章:更多的bash shell命令 监测程序 ps (其他ps内容见#1 ) Unix风格的ps命令参数 参数 描述 -A 显示所有进程 -N 显示与指定参数不符的所有进程 -a 显示除控制进程( ...

  5. bzoj 1097 [POI2007]旅游景点atr(最短路,状压DP)

    [题意] 给定一个n点m边的无向图,要求1开始n结束而且顺序经过k个点,给出经过关系x,y代表y必须在x之后经过,求最短路. [思路] 先对k个点进行spfa求出最短路. 设f[s][i]代表经过点集 ...

  6. Ubuntu 软件包管理详解

    原文转载自:http://www.cppblog.com/jb8164/archive/2009/01/09/71583.html Ubuntu 方便宜用,最值得让人称道的便是其安装软件的方式, 一条 ...

  7. POJ 1664 放苹果 (递推)

    题目链接:http://poj.org/problem?id=1664 dp[i][j]表示i个盘放j个苹果的方案数,dp[i][j] 可以由 dp[i - 1][j] 和 dp[i][j - i] ...

  8. HDU/杭电2013多校第三场解题报告

    今天悲剧了,各种被虐啊,还是太年轻了 Crime 这道题目给的时间好长,第一次就想到了暴力,结果华丽丽的TLE了. 后来找了一下,发现前24个是1, 2, 6, 12, 72, 72, 864, 17 ...

  9. HTML5学习笔记(一):HTML简介

    Web前端涵盖的内容较多且杂,主要由3个部分组成:HTML标记语言.CSS样式语言和JavaScript脚本语言组成,而下面我们将先学习最新的标记语言HTML5. <!DOCTYPE>标记 ...

  10. TypeScript学习笔记(三):类

    类 在TypeScript中,类似于C#的结构,即一个文件中可以存在多个类,且文件名可以任意取,我们先看一个简单的类的示例. class Person { private name: string; ...