递归建树,然后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. virtualenv 管理python 环境

    virualenvvirtualenv用于创建独立的Python环境,多个Python相互独立,互不影响,它能够:1. 在没有权限的情况下安装新套件2. 不同应用可以使用不同的套件版本3. 套件升级不 ...

  2. C#常用集合的使用

    大多数集合都在System.Collections,System.Collections.Generic两个命名空间.其中System.Collections.Generic专门用于泛型集合. 针对特 ...

  3. 调试minix内核

    调试的时候,要把电脑当作单片机来使用,故先需要添加串口,方便进行打印调试 http://wiki.minix3.org/doku.php?id=developersguide:serialout 1. ...

  4. perl的INC

    perl中的INC变量中包含了所有的perl module的查找路径. 查看@INC的值. 方式一: perl -V 方式二: % perl -e 'print join "\n" ...

  5. IOS传值之Block传值(二)

    @interface QWViewController : UIViewController @property(nonatomic,strong)UILabel *label; @property( ...

  6. h3c dhcp snooping

    1. 组网需求Switch B通过以太网端口Ethernet1/1连接到DHCP服务器,通过以太网端口Ethernet1/2.Ethernet1/3连接到DHCP客户端.要求:l与DHCP服务器相连的 ...

  7. Django urls常用匹配语法

    url from django.conf.urls import url from . import views urlpatterns = [ url(r'^articles/2003/$', vi ...

  8. Android开机动画启动流程

    android开机动画启动流程   从android的Surface Flinger服务启动分析知道,开机动画是在SurfaceFlinger实例通过调用startBootAnim()启动的. 下面我 ...

  9. JPA 系列教程2-单表操作

    JPA Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据.他的出现主要是为了简化现有的持久化开发工作和整合ORM技术,结束现在Hibe ...

  10. less分页阅读

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...