//题目 通过后续遍历 中序遍历 得出一棵树 ,然后按树的层次遍历打印

PS:以前对于这种用指针的题目是比较头痛的,现在做了一些链表操作后,感觉也不难

先通过后续中序建一棵树,然后通过BFS遍历这棵树

提供测试样例

4
4 1 3 2
2 3 1 4
10
10 7 6 9 8 5 4 1 3 2
7 10 6 2 5 9 8 3 1 4

//题目 通过后续遍历 中序遍历 得出一棵树 ,然后按树的层次遍历打印
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std; int back[];
int mid[];
int n; struct data{
int v;
int no;
data *left,*right;
}; data *head=new data;
data *rhead=head;
void build(){
int i,j;
head->v=back[n];
head->left=NULL;
head->right=NULL; int root=n,rj;
for(j=;j<=n;j++){
if(back[n]==mid[j]){
head->no=j;break;
}
}
for(i=n-;i>=;i--){
for(j=;j<=n;j++){
if(back[i]==mid[j]){
rj=j;break;
}
}
head=rhead;
while(){
if((rj < head->no)&&head->left!=NULL){
head=head->left;continue;
}
if((rj > head->no)&&head->right!=NULL){
head=head->right;continue;
}break;
}
if(rj < head->no){
head->left=new data;
head=head->left;
head->v=back[i];
head->no=rj;
head->left=NULL;
head->right=NULL;
}else{
head->right=new data;
head=head->right;
head->v=back[i];
head->no=rj;
head->left=NULL;
head->right=NULL;
}
} } void bfs(){
data *first,*second;
first=rhead;
queue<data *>qq;
qq.push(first); int ok=;
while(!qq.empty()){ if(ok==){
ok=;
printf("%d",qq.front()->v);
}else{
printf(" %d",qq.front()->v);
}
second=qq.front();
qq.pop();
if(second->left!=NULL){
qq.push(second->left);
}
if(second->right!=NULL){
qq.push(second->right);
}
}
printf("\n");
} int main()
{
while(scanf("%d",&n)!=EOF){
int i;
head=new data;
rhead=head;
for(i=;i<=n;i++){
scanf("%d",&back[i]);
}
for(i=;i<=n;i++){
scanf("%d",&mid[i]);
}
build();
bfs();
} return ;
}

1020. Tree Traversals (25) ——树的遍历的更多相关文章

  1. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  2. PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  3. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  4. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  5. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  6. 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

  7. 1020. Tree Traversals (25)

    the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020 and the ...

  8. 【PAT】1020. Tree Traversals (25)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  9. 1020. Tree Traversals (25) -BFS

    题目如下: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...

随机推荐

  1. Multiply Strings,字符串相乘

    问题描述:给定两个字符串,返回他们的乘积. public class MultiplyStrings { public String multiply(String num1, String num2 ...

  2. 监控系统信息模块psutil

    About psutil (python system and process utilities) is a cross-platform library for retrieving inform ...

  3. scala学习手记12 - 字段、方法和构造函数

    在上一节创建了一个scala类,如果没有更多的方法,scala类的定义还可以更简单一些,看一下下面这个CreditCard类的定义: class CreditCard(val number: Int, ...

  4. LightOJ - 1248 期望

    题意:有一个n面筛子,每次扔一下,每面概率相同,要求扔出n面的期望次数 题解:和第三篇论文里的例题一样,算从第i个到第i+1个的概率是(n-i)/n,n面中找n-i个没有扔到过的,期望是n/(n-i) ...

  5. jquery实现全选、不选、反选的两种方法

    在取复选框checkbox的属性checked属性值时,发现一个问题,就是当用attr取值时,真的为"checked",假的为"undefined";当用pro ...

  6. 一次完整的HTTP请求

    HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1:建立TCP连接,TCP的三次握手 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服 ...

  7. Myeclipse快捷键的设置以及默认的编码格式

    设置默认的编码格式

  8. 早上来开启eclipse,谁想代码都不见了,猜想是工作空间换了

    1.试了下网上说的改eclipse配置文件,不好使,连接地址:http://blog.csdn.net/gnail_oug/article/details/53992580 2.然后看了下 eclip ...

  9. react bootstrap-loader

    1.install npm install bootstrap-loader jquery resolve-url-loader 2.webpack.config.js entry: ['bootst ...

  10. padding和margin的用法

    在CSS中margin是指从自身边框到另一个容器边框之间的距离,就是容器外距离.在CSS中padding是指自身边框到自身内部另一个容器边框之间的距离,就是容器内距离. 一.padding 1.语法结 ...