1020 Tree Traversals (25 分)(二叉树的遍历)
给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历
#include<bits/stdc++.h> using namespace std;
const int N=;
int in[N];
int post[N]; typedef struct node;
typedef node *tree;
struct node
{
int data;
tree L;
tree R;
};
void print(tree &bt,int l1,int r1,int l2,int r2)
{
if(l1>r1||l2>r2) return;
int mid=l2;
while(in[mid]!=post[r1]) mid++;
bt=new node;
bt->data=post[r1];
bt->L=NULL;
bt->R=NULL;
print(bt->L,l1,l1+mid-l2-,l2,mid-);
print(bt->R,l1+mid-l2,r1-,mid+,r2);
}
vector<int>p;
void s(tree bt)
{
queue<tree>Q;
Q.push(bt);
while(!Q.empty()){
tree u=Q.front();
Q.pop();
p.push_back(u->data);
if(u->L) Q.push(u->L);
if(u->R) Q.push(u->R);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d",&post[i]);
for(int i=;i<n;i++) scanf("%d",&in[i]);
tree bt;
print(bt,,n-,,n-);
s(bt);
for(int i=;i<p.size();i++){
if(i) printf(" ");
printf("%d",p[i]);
}
printf("\n");
return ;
}
1020 Tree Traversals (25 分)(二叉树的遍历)的更多相关文章
- 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分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- 1020 Tree Traversals (25分)思路分析 + 满分代码
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 【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 Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 1020. Tree Traversals (25) ——树的遍历
//题目 通过后续遍历 中序遍历 得出一棵树 ,然后按树的层次遍历打印 PS:以前对于这种用指针的题目是比较头痛的,现在做了一些链表操作后,感觉也不难 先通过后续中序建一棵树,然后通过BFS遍历这棵树 ...
- 1020. Tree Traversals (25) -BFS
题目如下: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...
随机推荐
- 会话技术: Cookie Session JSP
## Cookie A..概念:客户端会话技术,将数据保存到客户端 B.使用步骤: 1.创建Cookie对象,绑定数据 new Cookie(String name, String value) ...
- 2017.10.6 Java命名规范及使用情况
Package 的命名 Package 的名字应该都是由一个小写单词组成. Class 的命名 Class 的名字必须由大写字母开头而其他字母都小写的单词组成 Class 变量的命名 变量的名字必须用 ...
- node.js 练习1
1.利用editplus 创建 n1.js 文件 2.输入代码 3.打开cmd 输入 node n1.js 4.打开浏览器 输入 localhost:8000 5.再次回看 cmd
- Spring Security 实现手机验证码登录
思路:参考用户名密码登录过滤器链,重写认证和授权 示例如下(该篇示例以精简为主,演示主要实现功能,全面完整版会在以后的博文中发出): 由于涉及内容较多,建议先复制到本地工程中,然后在细细研究. 1. ...
- python-一切事物都是对象
python:一切事物都是对象 开始接触python,在里面有一句话“一切事物都是对象”,那么如何来理解这句话呢,下面举简单的例子: a=1 b='hello't=(11,22,33) list1=[ ...
- word中磅和mm的换算
字号 磅 毫米数 初号 42 14.82 小初 36 12.70 一号 26 9.17 小一 24 8.47 二号 22 7.76 小二 18 6.35 三号 16 5.64 小三 15 5.29 四 ...
- 中小学信息学奥林匹克竞赛-理论知识考点--ASCII
ASCII表说白了就是一张表. 表中记录着:字符 和 数字 的对应关系.比如:字符0对应的ASCII码是48,A对应的是65,a对应的是97. 只要记住这三个,其它的数字,大写,小写字母的ASCII码 ...
- 访问数据库需要注意的问题 c#
在操作数据库的过程中,必然要产生数据库连接,这就要求在使用的时候要及时关闭连接.以避免数据库会话过多的问题. 以Oracle数据库为例: Oracle数据库查看会话,进程的语句 --查询数据库当前进程 ...
- spring开篇
本文引用http://www.cnblogs.com/ityouknow/p/5292559.html spring简介: spring是一个开源框架,spring是于2003 年兴起的一个轻量级的J ...
- myeclipse从SVN上拉项目,各种报错,jar包没有引入
问:项目中myeclipse从SVN上拉项目,各种报错,jar包没有引入 答: 从SVN拉项目步骤一定不能出错,一有点差异就会出非常多的事情 1-右键项目checkout的时候 第一页选默认值就行 点 ...