题目如下:

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

这是一道很直接的给出中序序列和任一其他序列生成二叉树的问题,本题给出的是后序遍历和中序遍历,利用后序遍历的“左右根”顺序我们知道,后序序列的最后一个元素一定是整棵树的根,从后向前,分别是右、左子树的根,因此通过后序序列可以找到一系列的根,他们的顺序是当前所在的根、右子树的根、左子树的根,每次在中序序列中定位出根的位置,根据中序序列“左根右”的顺序我们知道,根左边的一定是左子树,右边的一定是右子树,就这样递归解决子树问题即可,最后通过BFS来进行层序遍历。

具体实现方法为,设中序序列为inOrder,后序序列为postOrder,设置一个游标变量cur,左右范围变量left、right,cur作为一个全局变量,每次在postOrder中取出一个根,就让cur自减1,首先把拿到的根定位在inOrder中,设根所在的索引为rootIndex,首先建立当前根节点T,然后生成左子树范围left到rootIndex-1和右子树范围rootIndex+1到right,注意由于后序序列倒着走线碰到右子树,因此应该先递归T->right,再递归T->left,当发现left比right大时,说明没有子树,直接返回NULL,当发现left=right时,说明这是一个叶子结点,将结点的left和right置为NULL然后返回,最后一次递归返回时返回的是第一次创建的根结点,也就是整棵树的根,这时便得到了完整的二叉树。

接下来要进行层序遍历,只要对二叉树从根开始调用BFS即可,在结点出队时进行输出。

#include <iostream>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue> using namespace std; #define MAX 40 int postOrder[MAX];
int inOrder[MAX];
int N;
int cur; typedef struct TreeNode *Tree;
struct TreeNode{ Tree left;
Tree right;
int num; }; int findRootIndex(int rootNum){ for(int i = 0;i < N; i++){
if(inOrder[i] == rootNum){
return i;
}
}
return -1; } Tree CreateTree(int left, int right){
if(left > right ) return NULL;
int root = postOrder[cur];
cur --;
int rootIndex = findRootIndex(root);
Tree T = (Tree)malloc(sizeof(struct TreeNode));
T->num = root;
if(left == right){
T->left = NULL;
T->right = NULL;
}else{
T->right = CreateTree(rootIndex+1,right);
T->left = CreateTree(left,rootIndex-1);
}
return T; } void BFS(Tree T){ bool firstout = true;
queue<Tree> q;
q.push(T); while(!q.empty()){ Tree t = q.front();
q.pop();
if(firstout){
firstout = false;
cout << t->num;
}else{
cout << " " << t->num;
}
if(t->left != NULL){
q.push(t->left);
}
if(t->right!= NULL){
q.push(t->right);
} } } int main()
{
cin >> N;
cur = N-1;
for(int i = 0; i < N; i++){
cin >> postOrder[i];
}
for(int i = 0; i < N; i++){
cin >> inOrder[i];
} Tree T = CreateTree(0,cur); BFS(T); cout << endl; return 0;
}

1020. Tree Traversals (25) -BFS的更多相关文章

  1. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

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

  2. 【PAT】1020 Tree Traversals (25)(25 分)

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

  3. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

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

  4. PAT Advanced 1020 Tree Traversals (25 分)

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

  5. 1020. Tree Traversals (25)

    the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020 and the ...

  6. 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

  7. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  8. 【PAT】1020. Tree Traversals (25)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  9. PAT (Advanced Level) 1020. Tree Traversals (25)

    递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algo ...

随机推荐

  1. 利用mybatis-generator自动生成数据持久化的代码

    MyBatis生成器简介 MyBatis Generator(MBG)是MyBatis MyBatis 和iBATIS的代码生成器.它将生成所有版本的MyBatis的代码,以及版本2.2.0之后的iB ...

  2. RabbitMQ日志无法禁用问题

    最近使用spring+rabbitmq发现其Debug日志非常多,几天就把服务器磁盘弄爆了. 原来rabbitmq依赖logback.xml输出日志. 在和log4j.properties同目录下加一 ...

  3. 18. 4Sum(中等)

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  4. JAVA局部内部类

    在刚刚学到的android开发中了解到Button的onClick是通过局部内部类的方式实现的,具体的原理我以后再去了解,只是遇到问题总是想知道为什么,不要告诉我这是规则,死记住就可以了. 问题是局部 ...

  5. Tarjan笔记1

    Tarjan 2822 爱在心中 ** 时间限制: 1 s ** 空间限制: 128000 KB ** 题目等级 : 钻石 Diamond 题解 题目描述 Description"每个人都拥 ...

  6. 分布式服务框架Dubbo

    随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时,只需一个应用, ...

  7. iOS关于时间的处理

    转自:iOS关于时间的处理 做App避免不了要和时间打交道,关于时间的处理,里面有不少门道,远不是一行API调用,获取当前系统时间这么简单.我们需要了解与时间相关的各种API之间的差别,再因场景而异去 ...

  8. 计算机网络之文件传送协议FTP

    FTP 文件传送协议FTP(File Transfer Protocol)是因特网上使用最广泛的文件传送协议. FTP 提供交互式的访问,允许客户指明文件的类型与格式,并允许文件具有存取权限.FTP ...

  9. logistic分类

    对Logistic回归模型,个人做的一些总结: 公式就不套用了,教材上面基本都有而且详细.logistic回归用图形化形式描述如下: logistic回归是一种简单高效的分类模型,它不仅可以通过学习来 ...

  10. Quartz学习笔记1:Quartz概述

    Quartz是开源任务调度框架中的翘楚,它提供了强大的 任务调度机制.Quartz允许开发人员灵活的定义触发器的调度时间表,并可对触发器和任务进行关联映射.此外,Quartz提供了调度运行环境的持久化 ...