递归建树,然后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. cdn是什么

    CDN的全称是Content Delivery Network,即内容分发网络.其目的是通过在现有的Internet中增加一层新的网络架构, 将网站的内容发布到最接近用户的网络”边缘”,使用户可以就近 ...

  2. python--windows下安装BeautifulSoup

    python有很多内置的模块可以不安装使用,用起来非常方便,但是也有一些挺有用的非内置的模块不能直接使用,需要话费点力气手动安装. 进入python安装目录下的Scripts目录,查看是否有pip工具 ...

  3. fszipx.exe

    来源:http://www.funduc.com/fszipx.htm 是个免费软件,用于把.zip转化为.exe自解压文件. COPY /B "C:\Tools\FsZipX\FsZipX ...

  4. to_date()与to_char()

    1.以时间(Date类型)为查询条件时,可以用to_date函数实现: select t.* from D101 t where t.d101_40 = to_date('2013/9/12', 'y ...

  5. sql数据库删除表的外键约束(INSERT 语句与 FOREIGN KEY 约束"XXX"冲突。该冲突发生于数据库"XXX",表"XXX", column 'XXX)

    使用如下SQL语句查询出表中外键约束名称: 1 select name 2 from sys.foreign_key_columns f join sys.objects o on f.constra ...

  6. Local declaration of 'XXX' hides instance variable

    今天调试程序遇到这么一个警告! Local declaration of 'XXX' hides instance variable 遇到这种原因,是因为本地变量跟函数参数变量同名.改变其一即可.

  7. iOS字符串转化成CGFloat

    NSString *str = @"abc"; [str floatValue];

  8. php 判断是不是https链接

    如果当前运行的 PHP 版本等于或高于提供的版本号,该函数返回布尔值 TRUE ,反之则返回 FALSE . function is_https() { if (!empty($_SERVER['HT ...

  9. xp安装maven

    1.下载apache-maven-2.0.8 2.设置xp环境变量 MAVEN_HOME D:\apache-maven-2.0.8 在path里面假如  %MAVEN_HOME%\bin 然后打开c ...

  10. Swift 学习笔记(五)

    126. 协议(Protocols) 协议语法(Protocol Syntax) 属性要求(Property Requirements) 方法要求(Method Requirements) Mutat ...