您的位置:首页 > 其它

PAT甲级真题及训练集(19)--1020. Tree Traversals (25)(后序,中序建树,很重要)

2017-07-04 16:58 513 查看


1020. Tree Traversals (25)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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


提交代

/**
作者:一叶扁舟
时间:15:46 2017/7/4
思路:
说明给出一个二叉树的后序序列和中序序列,层次遍历出数据
思路,先根据后序和中序建树,然后层次遍历出二叉树
可以利用手工模拟,
例子:
7
后序: 2 3 1 5 7 6 4
中序: 1 2 3 4 5 6 7
输出:
4 1 6 3 5 7 2*/
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <math.h>
#include <vector>
using namespace std;
#define SIZE 101

typedef struct Node{
int data;
Node * leftChild;
Node * rightChild;
}Node;
//定义的全局变量,后序队列和,中序队列
int post[SIZE], middle[SIZE];
//定义的全局变量总共的结点数
int totalNode = 0;

/**
利用中序和后序序列递归进行建树
int postStart :后序队列起始坐标
int postEnd:后序队列终止坐标
int midStart :中序队列起始坐标
int midEnd :中序队列的终止坐标
*/

Node* create(int postStart, int postEnd, int midStart, int midEnd){
if (postStart > postEnd){
return NULL;
}

Node * root = new Node;//创建结点
//查找后序队列中,最后一个数据在中序队列中的位置
int i;
for (i = midStart; i <= midEnd; i++){
if (middle[i] == post[postEnd]){
//找到了,中序队列中第i个位置就是的
break;
}

}
//中序队列中距离的起始位置的个数,即第i个位置为根结点,左边num个是它的左子树的个数
int numStart = i - midStart;
root->data = post[postEnd];
root->leftChild = create(postStart, postStart + numStart - 1, midStart , i - 1);
root->rightChild = create(postStart + numStart, postEnd - 1, i + 1, midEnd);
return root;
}

//层次遍历二叉树
void level(Node * root,int totalNode){
int num = 0;
if (root == NULL){
return;
}
queue<Node*> q;
q.push(root);
while (!q.empty()){
//出队,然后访问该数据
Node * current = q.front();
q.pop();
printf("%d", current->data);
num++;
if (num < totalNode){
printf(" ");
}
if (current->leftChild != NULL){
q.push(current->leftChild);//左子树入队
}
if (current->rightChild != NULL){
q.push(current->rightChild);//右子树入队
}
}
return ;
}

int main(){
scanf("%d", &totalNode);
//后序序列
int temp;
for (int i = 0; i < totalNode; i++){
scanf("%d", &temp);
post[i] = temp;
}
//中序序列
for (int i = 0; i < totalNode; i++){
scanf("%d", &temp);
middle[i] = temp;
}
//根据后序和中序序列建二叉树
Node* root = create(0, totalNode - 1, 0 ,totalNode - 1);
//层次遍历二叉树
level(root, totalNode);

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息