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. OpenGL超级宝典第5版&&GLSL法线变换

    在GLSL中,有一些情况需要把局部坐标系下的向量或点转换到视点坐标系下,如光照计算时,需要把法向转化到视点坐标系.如果是模型上一点p 转化到视点坐标系下,直接(model-view matrix )* ...

  2. 关于duilib中的list的扩展探索

    原文地址:http://blog.csdn.net/tragicguy/article/details/21893065 今天在做一个程序的界面时,需要在一个列表中显示除文字以外的其他控件,如:Edi ...

  3. 【Linux学习】 包含子目录的makefile简单应用

    1 .目录结构 practice6 / ui / ui.h   ui.c practice6 / dal / dal.h dal.c practice6 / bll / bll.h  bll.c pr ...

  4. qt文本编辑器

    示例代码: mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include ...

  5. HDU5479 Colmerauer 单调栈+暴力优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5749 思路: bestcoder 84 贡献:所有可能的子矩阵的面积和 //len1:子矩阵所有长的和 ;i&l ...

  6. SpringMVC+Spring+Hibernate+Maven+mysql整合

    一.准备工作 1.工具:jdk1.7.0_80(64)+tomcat7.0.68+myeclipse10.6+mysql-5.5.48-win322. 开发环境安装配置.Maven项目创建(参考:ht ...

  7. POJ 1308 Is It A Tree? (并查集)

    Is It A Tree? 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/M Description A tree is a w ...

  8. UVA 10054 The Necklace(欧拉回路,打印路径)

    题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. springboot+maven快速构建项目

    最近公司运用springboot构建项目,确实比ssh搭建要快很多.springboot官方学习网站 1.首先要下载maven,用maven管理项目很方便,下载完maven配置好环境,maven我就不 ...

  10. 强连通分量(tarjan求强连通分量)

    双DFS方法就是正dfs扫一遍,然后将边反向dfs扫一遍.<挑战程序设计>上有说明. 双dfs代码: #include <iostream> #include <cstd ...