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

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. linux 进阶命令___0001

    查看指定目录下最大的文件 #查看/var目录下前10个最大的文件 #Find top 10 largest files in /var directory (subdirectories and hi ...

  2. IE10下阿里旺旺无法快速登录解决办法

    一直都是用Chrome浏览器的,如果已经登录了旺旺,打开淘宝登录的时候都是会有快速登录的,免得手工输入用户名密码了.不经意间用IE10打开淘宝,登录时发现无法使用快速登录,一番研究后发现,IE做了保护 ...

  3. FZU 1759 Super A^B mod C 指数循环节

    Problem 1759 Super A^B mod C Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description G ...

  4. scjp考试准备 - 8 - final关键字

    题目,如下代码的执行结果: import java.util.ArrayList; class Pizza{ ArrayList toppings; public final void addTopp ...

  5. virtio,vhost 和vhost-user

    随着qemu2.1的发布,可以看到,qemu支持了vhost-user.从介绍可以看出,这是把原来vhost-backend从kernel移到了userspace,这和原来virtio架构有什么区别呢 ...

  6. Shell 概述、截取字符操作等

    Shell 是用C语言编写的程序,便于用户与Linux内核系统进行交互. Linux的Shell种类众多,常见的有: Bourne Again Shell (/bin/bash) Bourne She ...

  7. opencv:基本图形绘制

    可以使用opencv绘制 直线.圆.方形.椭圆等基本图形. 示例代码: #include <opencv.hpp> using namespace cv; int main() { // ...

  8. listView使用小技巧P66--P76

    1.设置分割线高度和颜色 android:divider="@android:color/darker_gray" android:dividerHeight="10dp ...

  9. Struts06---通配符的使用

    01.创建对应的login.jsp页面 <%@ page language="java" import="java.util.*" pageEncodin ...

  10. 微信oauth2验证

    微信公众号进行oauth2验证时的要求: 需要两个地方配置: (1)   Oauth2验证链接中的appid.redirecturi (2)   微信公众号授权回调页面域名: Appid为公众号的ap ...