PAT (Advanced Level) 1020. Tree Traversals (25)
递归建树,然后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)的更多相关文章
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- PAT (Advanced Level) 1086. Tree Traversals Again (25)
入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include&l ...
- 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 【PAT】1020. Tree Traversals (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
随机推荐
- 深入浅出Koa
深入浅出Koa(1):生成器和Thunk函数 Koa是个小而美的Node.js web框架,它由Express的原班人马打造的, 致力于以一种现代化开发的方式构建web应用. 通过这个系列,你将能够理 ...
- device-mapper: multipath: Failing path recovery【转载】
digoal 2016-04-05 10:09:42 浏览180 评论0 摘要: 由于扇区损坏导致多路径设备failed. 现象如下 : # dmesg : device-mapper: mul ...
- Android Studio 如何使用jni
在project视图下,main文件夹下,创建jniLibs文件夹,然后把so文件放入即可:
- IO模式设置网络编程常见问题总结—IO模式设置,阻塞与非阻塞的比较,recv参数对性能的影响—O_NONBLOCK(open使用)、IPC_NOWAIT(msgrcv)、MSG_DONTWAIT(re
非阻塞IO 和阻塞IO: 在网络编程中对于一个网络句柄会遇到阻塞IO 和非阻塞IO 的概念, 这里对于这两种socket 先做一下说明: 基本概念: 阻塞IO:: socket 的阻塞模式 ...
- SmtpDlg 调用SMTP
// SmtpDlg.h : 头文件 // #pragma once #include "afxwin.h" #include "string" using n ...
- 根据View获取该控制器
//根据View获取控制器 - (UIViewController*)viewController { for (UIView* next = [self superview]; next; next ...
- WPF(ContentControl和ItemsControl)
WPF(ContentControl和ItemsControl) 2013-04-01 16:25 2188人阅读 评论(0) 收藏 举报 分类: .Net(C#)(31) WPF(25) 版权 ...
- Windows下将ImageMagick移植到Android平台
Windows下将ImageMagick移植到Android平台 原文链接 http://www.pedant.cn/2014/06/18/imagemagick-ported-android/ I ...
- 使用devcon禁用启用网卡
系统平台:win2003 情况描述: 机器上装有两块网卡,8136和8139,网卡A使用静态IP,连接内部办公网,网卡B使用DHCP,连接互联网.切换两个网络时,需要先禁用一个网卡,启用另一个网卡.来 ...
- Dom++完美版得到元素到html的距离6/4/21
function getTop(obj) { var pos={left:0,top:0}; while(obj) { pos.left+=obj.offsetLeft; pos.top+=obj.o ...