L2-011. 玩转二叉树
2016-07-18 16:38
232 查看
题目链接:L2-011. 玩转二叉树
和之前的题一样,只需要改动一点。
和之前的题一样,只需要改动一点。
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <queue> using namespace std; const int maxn = 1e3+5; int a[maxn]; int b[maxn]; int floor1[maxn]; int tr[maxn<<2]; int retr[maxn<<2]; int n; void build(int x,int al,int ar,int bl,int br) { if(ar<al||br<bl) return; if(al == ar) { tr[x] = a[al]; return; } for(int i = 1; i<=n; i++) { if(a[i]==b[bl]) { tr[x] = a[i]; build(2*x,i+1,ar,bl+i-al+1,br); //只需要将之前的代码上下两句调换即可。 build(2*x+1,al,i-1,bl+1,bl+i-al); break; } } } void bfs(int x) //按层序遍历 { queue<int> q; q.push(x); int ans = 1; while(!q.empty()) { int y = q.front(); q.pop(); if(tr[y]==0) continue; floor1[ans++] = tr[y]; q.push(2*y); q.push(2*y+1); } } int main() { memset(tr,0,sizeof(tr)); scanf("%d",&n); for(int i = 1; i<=n; i++) { scanf("%d",&a[i]); } for(int i = 1; i<=n; i++) { scanf("%d",&b[i]); } build(1,1,n,1,n); retr[1] = tr[1]; bfs(1); for(int i = 1; i<n; i++) printf("%d ",floor1[i]); printf("%d\n",floor1 ); return 0; }
相关文章推荐
- ORACLE AWR报告生成过程出现多个实例记录分析
- Codeforces Round #353 (Div. 2) A - Infinite Sequence
- 学习笔记-斯坦福iOS7-第十四课:UIApplication和MapKit
- 预编译头文件之pch文件的创建及使用
- git pull
- JsonCpp使用细谈(Windows平台)
- Android 7(N)网络安全配置
- Executor相关的一些类
- MySQL两种存储引擎MyISAM于InnoDB对比
- STM32三种启动模式
- android session的使用(图片验证码)
- HDU5475——An easy problem(线段树or暴力)
- 动画原理 浅析
- 【C语言入门教程】7.5 枚举
- Xcode8安装使用以及新特性
- caffe中base_lr、blobs_lr和lr_policy
- 自定义对话框
- 《韩非子》的法
- Java - 数据库(mysql - 3)
- 企业级云应用平台的实践和思考