1020. Tree Traversals
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
本题考查树的遍历, 给出后根遍历和中根遍历,可以确定一棵树, 最后使用层次遍历获取LevelOrder,思路如下:
直接上例子, 题目给出后根遍历2 3 1 5 7 6 4, 中根遍历1 2 3 4 5 6 7
后跟遍历可知, 4是整棵树的跟, 再看中跟遍历中4的位置,把整棵树分成1,2,3组成的左子树和5,6, 7组成的右子树, 同理, 后根遍历中也会分成2,3,1 和5,7,6两颗子树, 此时,后根2,3,1对应中根1 2 3, 后根5,7,6对应中跟5,6,7可继续递归求解,但是要注意判断特殊情况:
- 确定根后, 发现根在中根遍历的最右边,那么此书右子树为空
- 确定根后, 发现根在中根遍历的最左边,那么此书左子树为空
#include <iostream>
#include <queue>
using namespace std;
typedef struct
{
int value;
int left;
int right;
} Node;
Node node[32];
int postOrder[32];//后跟遍历
int midOrder[32];//中跟遍历
int N;
//递归函数, l1,r1表示后跟遍历的左界和右界
//l2, r2表示中根遍历的左界和右界
void f(int l1, int r1, int l2, int r2)
{
int root = postOrder[r1];
if(l1 == r1)
{
node[root].left = node[root].right = -1;
return;
}
//i记录根在midOrder的位置
int i = 0;
while(midOrder[i] != root) i++;
int cntL = i - l2; //cntL表示i左边有几个元素,用于分隔postOrder
if(i == l2)//左子树为空情况
{
node[root].left = -1;
node[root].right = postOrder[r1 - 1];
f(l1 + cntL, r1 - 1, i + 1, r2);
return;
}
if(i == r2)//右子树为空情况
{
node[root].right = -1;
node[root].left = postOrder[r1 - 1];
f(l1, l1 + cntL - 1, l2, i - 1);
return;
}
//两边都不为空,先求解两根
f(l1, l1 + cntL - 1, l2, i - 1);
f(l1 + cntL, r1 - 1, i + 1, r2);
//最后给根赋值
node[root].left = postOrder[l1 + cntL - 1];
node[root].right = postOrder[r1 - 1];
}
//层次遍历
void bfs()
{
int root = postOrder[N - 1];
queue<int> q;
q.push(root);
int first = 1;
while(!q.empty())
{
int v = q.front();
if(first)
{
first = 0;
cout << v;
}
else
{
cout << " " << v;
}
q.pop();
if(node[v].left != -1)
q.push(node[v].left);
if(node[v].right != -1)
q.push(node[v].right);
}
}
int main()
{
cin >> N;
for(int i = 0; i < N; i++)
{
cin >> postOrder[i];
}
for(int i = 0; i < N; i++)
{
cin >> midOrder[i];
}
f(0, N - 1, 0, N - 1);
bfs();
return 0;
}
1020. Tree Traversals的更多相关文章
- 【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[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- PAT 1020. Tree Traversals
PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...
- 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 ...
- 1020 Tree Traversals——PAT甲级真题
1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- 1020. Tree Traversals (25)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020 and the ...
随机推荐
- vuejs数据双向绑定原理(get & set)
前端的数据双向绑定指的是view(视图)和model(数据)两者之间的关系:view层是页面上展示给用户看的信息,model层一般是指通过http请求从后台返回的数据.view到model的绑定都是通 ...
- Selenium Web 自动化 - 项目持续集成(进阶)
Selenium Web 自动化 - 项目持续集成(进阶) 2017-03-09 目录 1 背景及目标2 环境配置 2.1 SVN的安装及使用 2.2 新建Jenkins任务3 过程分析 1 背景 ...
- DAX/PowerBI系列 - 参数表(Parameter Table)
DAX/PowerBI系列 - 参数表(Parameter Table) 难度: ★☆☆☆☆(1星) 适用范围: ★★★★☆(4星) 概况: 这个模式比较简单灵活,而且很实用.所用的DAX语句也比较简 ...
- Android Material Design 系列之 SnackBar详解
SnackBar是google Material Design提供的一种轻量级反馈组件.支持从布局的底部显示一个简洁的提示信息,支持手动滑动取消操作,同时在同一个时间内只能显示一个SnackBar. ...
- JAVA发送邮件的DEMO
最近有朋友问邮件怎么发送,就简单写了个demo,因为懒得找jar包,所以项目是创建的maven工程,具体的maven引用的jar如下: <dependency> <groupId&g ...
- SignalR指定用户推送消息
一.首先,在MVC项目中安装SingalR包(SingalR2.0需要.net4.5以上,VS2010可以安装1.1.3版本,本例为VS2010+SignalR1.1.3). 打开工具-NuGet程序 ...
- mysql---数据控制语言(用户及其权限管理)
用户管理 用户数据所在位置: mysql中的所有用户,都存储在系统数据库(mysql)中的user 表中--不管哪个数据库的用户,都存储在这里.
- 用 Visual Studio Code 调试 Node.js
环境: Visual Studio Code Node.js 1. 关闭运行中的程序 2.打开入口文件,我这里的入口文件为 app.js 3.点击左侧菜单栏的 debug 按钮 4.点击运行按钮 5 ...
- yii2.0自带验证码使用
相信大家刚接触yii框架的时候都会觉得它比较麻烦.很不顺手.但我相信,只要你使用过一段时间后就会发觉它超给力,很高大上.里面的小物件很多,让你随心所欲的使用. 自带验证码实现也挺简单的,代码如下 1. ...
- iOS开发之数据存储之Core Data
1.概述 Core Data框架提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite3数据库文件中,也能够将保存在数据库中的数据还原成OC对象.在此数据操作期间,不需要 ...