递归建树,然后BFS一下

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std; const int maxn=;
int a[maxn],b[maxn];
int n,tot;
struct Node
{
int left;
int right;
int val;
}node[maxn]; void build(int L,int R,int fa,int f)
{
int P=-;
for(int i=L;i<=R;i++)
for(int j=;j<=n;j++)
if(b[i]==a[j]) P=max(P,j); int root_val=a[P]; if(tot==)
{
++tot;
node[tot].val=root_val;
}
else if(f==)
{
++tot;
node[tot].val=root_val;
node[fa].left=tot;
}
else if(f==)
{
++tot;
node[tot].val=root_val;
node[fa].right=tot;
} int tmp=tot; for(int i=L;i<=R;i++)
{
if(b[i]==root_val)
{
if(i-L>) build(L,i-,tmp,);
if(R-i>) build(i+,R,tmp,);
break;
}
} } void bfs()
{
queue<int>Q;
Q.push();
int cnt=;
while(!Q.empty())
{
int head=Q.front(); Q.pop();
printf("%d",node[head].val); cnt++;
if(cnt<n) printf(" ");
else printf("\n");
if(node[head].left!=-) Q.push(node[head].left);
if(node[head].right!=-) Q.push(node[head].right);
}
} int main()
{
scanf("%d",&n); tot=;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=n;i++) scanf("%d",&b[i]);
for(int i=;i<=;i++) node[i].left=node[i].right=-;
build(,n,-,-);
bfs();
return ;
}

PAT (Advanced Level) 1020. Tree Traversals (25)的更多相关文章

  1. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

  2. PAT (Advanced Level) 1086. Tree Traversals Again (25)

    入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include&l ...

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

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

  4. PAT Advanced 1020 Tree Traversals (25 分)

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. linq中的分组和排序

    一.分组  group 组内成员 by 分组条件 into 组的信息 class Program { static void Main(string[] args) { string[] name = ...

  2. namenode ha

    http://blog.csdn.net/totxian/article/details/45248399 http://www.aboutyun.com/thread-13679-1-1.html ...

  3. on-tap和on-click

    简单的说,当你点击移动设备屏幕上的一个点之后,on-tap会立刻触发,而on-click(可能)需要等待300ms才触发——这是移动设备浏览器为了检测是否存在双击的一个检测周期长度. 在移动设备上,应 ...

  4. block 的演练和使用

    概念 block 是 C 语言的 是一种数据类型,可以当作参数传递 是一组预先准备好的代码,在需要的时候执行 动画 block 回顾 self.demoView.center = CGPointMak ...

  5. Object对象

    1.Object类:所有类的根类.是不断抽取而来的,具备所有对象都具有的共性内容.其中的方法,任何对象都可以调用.继承而来的. equals()方法: Object类的equals源码:比较两个对象是 ...

  6. Swift & OC 混编 浅析

    转载自:http://www.infoq.com/cn/articles/wangyi-cartoon-swift-mixed-practice?utm_campaign=rightbar_v2&am ...

  7. Swift 学习笔记 (一)

    原创: 转载请注明出处 Extention try catch rxSwift internal  public  private var  let as       as? 强转 ? ! didSe ...

  8. Java-枚举介绍

    需求:今天遇到一个问题,就是返回某些固定的int值,要用到枚举. 下面开始介绍: 无参构造方法的枚举 enum Color{ YELLOW,BLUE,RED } 解析:首先Color本身是一个枚举,里 ...

  9. 初识Selenium(二)

    ---------------------------------------------------------------------------------------------------- ...

  10. 基础-session,cookie,jsp,EL,JSTL

    会话可以简单理解为:用户打开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. kookie是在服务器端创建的,返回给浏览器,在浏览器的目录中保存了,下一次 ...