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 ...
随机推荐
- sql语句,实践证明了某种情况下not in的效率高于not exists
只要百度not in和not exists,清一色的not exists的效率优于not in,毕竟not exists只是去强调是否返回结果集,只是一个bool值,而not in是返回一个结果集,是 ...
- html基础知识2(有序无序列表,表格)2017-03-08
摘要:php 基础知识2 重点:有序无序列表:<a>标签:<table>标签 内容容器 1.段落标签 <p></p> 注: 执行前后换行,并空一行 ...
- SFTP远程文件上传
远程服务器remote_host=192.168.29.142用户为remote_www,用户当前目录为/home/remote_www 本地服务器local_host=192.168.29.135用 ...
- F# 语法概览
F#和C#的语法差别 语法上,F#和C#有两个主要差别: 用缩进而非花括号分隔代码块 用空白而非逗号分隔参数 F#常见语法元素 以下是F#代码中常见的语法元素 注释 // 这是单行注释 (* 这是多行 ...
- node c++多线程插件 第二天 c++指针
虽然取名叫node多线程插件,但是目前还是在学习c++的情况. 今天谈一谈c++指针. c++指针就像是c#中的引用变量,例如一个Person类的实例zs{Name="张三",Ag ...
- 说说MySQL中的Redo log Undo log都在干啥
在数据库系统中,既有存放数据的文件,也有存放日志的文件.日志在内存中也是有缓存Log buffer,也有磁盘文件log file,本文主要描述存放日志的文件. MySQL中的日志文件, ...
- CDbConnection failed to open the DB connection
打开数据库失败 有我遇到的有寄给问题: 1 Not find Drive 2 SQLSTATE[28000] [1045] Access denied for user 'xxx'@'localhos ...
- python + selenium <三>
sql 数据库连接 引用pymssql模块 import pymssqldef getDB(name,psw,dbname,sql): conn=pymssql.connect(HOST=host,N ...
- 降低Redis内存占用
1.降低redis内存占用的优点 1.有助于减少创建快照和加载快照所用的时间 2.提升载入AOF文件和重写AOF文件时的效率 3.缩短从服务器进行同步所需的时间 4.无需添加额外的硬件就可以让redi ...
- HTML学习笔记汇总
笔记几乎涵盖了日常开发中全部的知识点以及相关注意事项 想要学习网页制作的初学者可以通过本篇笔记初步掌握HTML的使用,也可以将该笔记作为查阅资料查看 HTML简单结构 <html> < ...