【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 ...
 
随机推荐
- 使用Phalcon开发工具碰到的数据库问题"Table 'XXX' doesn't exist in database when dumping meta-data for XXX"
			
使用Phalcon开发工具,通过命令行生成程序框架 设置好config.php,在对数据库进行读取.保存数据的时候出现了问题“Table 'XXX' doesn't exist in database ...
 - POJ 3422 Kaka's Matrix Travels (K取方格数:最大费用流)
			
题意 给出一个n*n大小的矩阵,要求从左上角走到右下角,每次只能向下走或者向右走并取数,某位置取过数之后就只为数值0,现在求解从左上角到右下角走K次的最大值. 思路 经典的费用流模型:K取方格数. 构 ...
 - Mysql使用大全
			
#登录数据库 mysql -hlocalhost -uroot -p; #修改密码 mysqladmin -uroot -pold password new; #显示数据库 show database ...
 - Windows 小技巧: 變更輸入法順序
			
Windows XP 中還是有辦法變更輸入法順序的!!只不過,要動用到 Regedit.exe 這個程式. 執行 Regedit.exe至 HKEY_CURRENT_USER\Keyboard Lay ...
 - in 和 exist 区别
			
select * from Awhere id in(select id from B) 以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存起来.之后,检查A表的id是否与B ...
 - mysql添加用户和用户权限
			
Mysql添加用户使用可以对mysql数据库用户表有操作权限的用户名登陆mysqlinsert into user(Host,User,Password) values('%','name','pas ...
 - 在Linux系统中如何装rpm,deb,tar.gz,tar.bz2,apt,bin 格式的文件
			
首先安装 系统自带的 alien 包 :终端 -su-输入密码 -进入ROOT 用户 - sudo apt-get install alien 这样 alien 包 就装上去了 !(if alien ...
 - selenium + python 多浏览器测试
			
selenium + python 多浏览器测试 支持库包 在学习 Python + Selenium 正篇之前,先来看下对多浏览器模拟的支持.目前selenium包中已包含webdriver,hel ...
 - 代理抓取RSS信息
			
最近工作很闲,就自己写了一个可以看RSS订阅的网站.话说,RSS阅读器到处都是,随便下一个就可以了,为什么还去做一个网站形式的呢?作为一个热(xian)爱(de)前(dan)端(teng)的程序员,我 ...
 - 使用java开源工具httpClient及jsoup抓取解析网页数据
			
今天做项目的时候遇到这样一个需求,需要在网页上展示今日黄历信息,数据格式如下 公历时间:2016年04月11日 星期一 农历时间:猴年三月初五 天干地支:丙申年 壬辰月 癸亥日 宜:求子 祈福 开光 ...