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 ...
随机推荐
- OC画笔CGContextRef
1.画线 CGContextRef context = UIGraphicsGetCurrentContext();//context相当于画布 CGContextSetStrokeColorWith ...
- freemarker中的list 前端模板
freemarker list (长度,遍历,下标,嵌套,排序)1. freemarker获取list的size : JavaArrayList<String> list = new Ar ...
- fatal error: gst/gst.h
ln命令使用 ln -s 源文件(src) 目标文件(dest) 进到这个文件:~/LowDA/sysroots/mx6q/usr/include$ ln -s gstreamer-0.10/gs ...
- EL表达式处理字符串 是否 包含 某字符串 截取 拆分...............
EL表达式处理字符串 是否 包含 某字符串 截取 拆分............... JSP页面页头添加<%@ taglib uri="/WEB-INF/taglib/c.tld&qu ...
- Newly Setting up a CentOS-7 system
yum install -y epel-release glibc.i686 libtools vim clang git autoconf automake w3m glibc screen the ...
- iOS 程序性能优化
前言 转载自:http://www.samirchen.com/ios-performance-optimization/ 程序性能优化不应该是一件放在功能完成之后的事,对性能的概念应该从我们一开始写 ...
- 二叉树,平衡树,红黑树,B~/B+树汇总
二叉查找树(BST),平衡二叉查找树(AVL),红黑树(RBT),B~/B+树(B-tree).这四种树都具备下面几个优势: (1) 都是动态结构.在删除,插入操作的时候,都不需要彻底重建原始的索引树 ...
- HDU 2671 Can't be easier
简单的几何题目 点(a,b)关于直线Ax+By+C=1对称点的公式 #include<cstdio> #include<cstring> #include<cmath&g ...
- InnoDB的数据页结构
页是InnoDB存储引擎管理数据库的最小磁盘单位.页类型为B-tree node的页,存放的即是表中行的实际数据了. InnoDB数据页由以下七个部分组成,如图所示: File Header(文件头) ...
- PHP无限极分类的几种方法
导读:项目开发,经常栏目要做到无限极分类,几种方法PHP无限极分类的几种方法 复制代码 代码如下:namespace Util;class Category{static public functio ...