【PAT】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
分析:考察树的建立和遍历。
课参考《编程之美》3.9
#include<iostream>
#include<map>
#include<vector>
#include<queue>
using namespace std; struct Node{
Node *left;
Node *right;
int value;
Node():left(NULL),right(NULL){}
}; void Rebuild(int * PostOrder, int * InOrder, int len, Node* &root){
//判断何时结束递归
if(PostOrder == NULL || InOrder == NULL)
{
root = NULL;
return ;
}
if(root == NULL) root = new Node;
root->value = *(PostOrder + len - 1);
root->left = NULL;
root->right = NULL;
if(len == 1)
return; int count = 0;
int *temp = InOrder;
while(*temp != *(PostOrder + len -1))
{
count ++;
temp++;
if(count > len) break;
}
int left = temp - InOrder ;
int right = len - left - 1;
if(left > 0)
Rebuild(PostOrder, InOrder, left, root->left);
if(right > 0)
Rebuild(PostOrder + left, InOrder+left+1, right, root->right);
} int main()
{
int n,i,t;
while(cin>>n)
{
int *PostOrder = new int[n];
int *InOrder = new int[n];
for(i=0; i<n; i++)
cin>>PostOrder[i];
for(i=0; i<n; i++)
cin>>InOrder[i]; Node *root = new Node;
int post_start,in_start;
post_start = 0;
in_start = 0;
Rebuild(PostOrder, InOrder, n, root);
queue<Node *> q;
q.push(root);
int flag = 1; while(!q.empty()){
if(q.front()->left != NULL)
q.push(q.front()->left);
if(q.front()->right != NULL)
q.push(q.front()->right);
if(flag != n)
cout<<q.front()->value<<" ";
else
cout<<q.front()->value;
flag ++;
q.pop();
} cout<<endl;
}
return 0;
}
【PAT】1020. Tree Traversals (25)的更多相关文章
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- PAT (Advanced Level) 1020. Tree Traversals (25)
递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algo ...
- 1020. Tree Traversals (25)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020 and the ...
随机推荐
- IOS中设置cell的背景view和选中时的背景view 、设置cell最右边的指示器(比如箭头\文本标签)
一.Cell的设置 1.设置cell的背景view和选中时的背景view UIImageView *bg = [[UIImageView alloc] init]; bg.image = [UIIma ...
- web.xml整合s2sh内容
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
- aspose.words复制插入同一word文档中的某个页面
选择word模板 Document doc = new Document(Server.MapPath("~\\templet") + "\\" + name. ...
- Arduino开发常见错误
使用Ethernet时需要指定访问服务器的ip,我用的是本机做服务器.但是有一天重启了路由器,ip地址就变了!程序得跟着改! Arduino突然烧写不了程序:可能是正在运行的程序让arduino死机了 ...
- myeclipse10添加jQuery自动提示
首先先要在装上spket插件,这个网上有好多教程,我就不详细说了,主要说一下后面的设置,因为我发现我按照网上的装完也设置完没办法使用自动提示功能,以下是我根据前辈的经验然后自己摸索出来的: 选中所建的 ...
- Java中的OutOfMemoryError的各种情况及解决和JVM内存结构
在JVM中内存一共有3种:Heap(堆内存),Non-Heap(非堆内存) [3]和Native(本地内存). [1] 堆内存是运行时分配所有类实例和数组的一块内存区域.非堆内存包含方法区和JVM内部 ...
- android操作SQLite
一.SQLite SQLite是一种转为嵌入式设备设计的轻型数据库,其只有五种数据类型,分别是: NULL: 空值 INTEGER: 整数 REAL: 浮点数 TEXT: 字符串 BLOB: 大数据 ...
- Java 与无符号那些事儿
最近在使用 Java 作为 WebSocket 客户端连接 Node.js 的 WebSocket 服务器的时候,由于使用的客户端库比较老,所以遇到了字节符号的问题,上网查了一下,看到这篇文章写的很有 ...
- 使用python三方库xlrd解析excel数据
excel是平常用的比较多的一种数据格式,而在自动化测试过程中,解析其数据以供脚本使用就是一个重要的工作,幸好已有现存的三方库供使用,而不必重新造轮子. 一.安装xlrd模块 到python官网下载h ...
- SQL Server 最小化日志操作解析,应用[手稿]
Sql Server 中数据库在BULK_LOGGED/SIMPLE模式下的一些操作会采用最小化日志的记录方式,以减小tran log落盘日志量从而提高整体性能. 这里我简单介绍下哪些操作在什么样的情 ...