题目链接:L2-006. 树的遍历

今天一神给我手敲二叉树模板,瞬间就领悟了,感觉自己萌萌哒!

看上去很直观!

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1e3+;
int a[maxn];
int b[maxn];
int floor[maxn];
int tr[maxn<<];
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 = al; i<=ar; i++)
{
if(a[i]==b[br])
{
tr[x] = b[br];
build( * x , al , i - , bl , bl + i - - al); //左子树的左端点,右端点,在a数组和b数组。
build( * x + , i + , ar , bl + i - al, br - ); //右子树的左端点,右端点,在a数组和b数组。
break;
}
}
}
/*void dfs(int x) //按先序遍历
{
if(!tr[x]) return;
printf("%d ",tr[x]);
dfs(2*x);
dfs(2*x+1);
}*/
void bfs(int x) //按层序遍历
{
queue<int> q;
q.push(x);
int ans = ;
while(!q.empty())
{
int y = q.front();
q.pop();
if(tr[y]==) continue;
floor[ans++] = tr[y];
q.push(*y);
q.push(*y+);
}
}
int main()
{
int n;
memset(tr,,sizeof(tr));
scanf("%d",&n);
for(int i = ; i<=n; i++)
{
scanf("%d",&b[i]);
}
for(int i = ; i<=n; i++)
{
scanf("%d",&a[i]);
}
build(,,n,,n);
//dfs(1);
bfs();
for(int i = ; i<n; i++) printf("%d ",floor[i]);
printf("%d\n",floor[n]);
return ;
}

只求先序时,不用遍历。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1e3+;
int a[maxn];
int b[maxn];
vector<int> g;
void build(int x,int al,int ar,int bl,int br)
{
if(ar<al||br<bl) return;
if(al==ar)
{
g.push_back(a[al]);
return;
}
for(int i = al; i<=ar; i++)
{
if(a[i]==b[br])
{
g.push_back(a[i]);
build( * x , al , i - , bl , bl + i - - al);
build( * x + , i + , ar , bl + i - al, br - );
break;
}
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i = ; i<=n; i++)
{
scanf("%d",&b[i]);
}
for(int i = ; i<=n; i++)
{
scanf("%d",&a[i]);
}
build(,,n,,n);
for(int i = ; i<g.size()-; i++) printf("%d ",g[i]);
printf("%d\n",g[g.size()-1]);
return ;
}

L2-006. 树的遍历的更多相关文章

  1. GPTL—练习集—006树的遍历

    #include<bits/stdc++.h> using namespace std; typedef int daTp;//datatype typedef struct BTNode ...

  2. 天梯 L2 树的遍历(已知后序中序求层序)

    树的遍历 (25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数.第二行 ...

  3. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  4. javascript实现数据结构: 树和二叉树的应用--最优二叉树(赫夫曼树),回溯法与树的遍历--求集合幂集及八皇后问题

    赫夫曼树及其应用 赫夫曼(Huffman)树又称最优树,是一类带权路径长度最短的树,有着广泛的应用. 最优二叉树(Huffman树) 1 基本概念 ① 结点路径:从树中一个结点到另一个结点的之间的分支 ...

  5. PTA 7-10 树的遍历(二叉树基础、层序遍历、STL初体验之queue)

    7-10 树的遍历(25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数 ...

  6. 数据结构--树(遍历,红黑,B树)

    平时接触树还比较少,写一篇博文来积累一下树的相关知识. 很早之前在数据结构里面学的树的遍历. 前序遍历:根节点->左子树->右子树 中序遍历:左子树->根节点->右子树 后序遍 ...

  7. YTU 3023: 树的遍历

    原文链接:https://www.dreamwings.cn/ytu3023/2617.html 3023: 树的遍历 时间限制: 1 Sec  内存限制: 128 MB 提交: 3  解决: 2 题 ...

  8. 团体程序设计天梯赛-练习集L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  9. leetcode404-----简单的树的遍历

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  10. pat L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

随机推荐

  1. 多选出差同事id,拼接,去掉最后逗号

    ===========方法1 substr() ,永远都是.(第一个参数)开始位置.(第二个参数)截取个数 ,负数表示都后面开始数 substr($data['members'],0,strlen($ ...

  2. angular跨域访问的问题

    CORS跨域资源共享 跨域资源共享(CORS )是一种网络浏览器的技术规范,它为Web服务器定义了一种方式,允许网页从不同的域访问其资源. Form responseHeaders = (Form) ...

  3. 快速玩转Apple Pay开发

    快速玩转Apple Pay开发 更新时间:2016年02月20日09时25分 来源:传智播客 Apple Pay 2016年2月18日上午,苹果公司宣布,与中国银联达成合作,正式在中国大陆上线Appl ...

  4. poi 合并单元格、设置边框

    HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...

  5. Spring中管理Bean以及解析XML

    Spring是分层的轻量级框架 以IoC(Inverse of Control 反转控制)和AOP(Aspect Oriented Programming 面向切面编程)为核心 应用Spring的好处 ...

  6. gameUnity 网络游戏框架

    常常在想,有没有好的方式,让开发变得简单,让团队合作更加容易. 于是,某一天 动手写一个 架构, 目前版本 暂定 0.1 版本.(unity5.0.0f4 版本以上) 我打算 开源出来 0.1有什么功 ...

  7. 为什么Hbase能实现快速的查询

    你的快速是指什么? 是根据亿级的记录中快速查询,还是说以实时的方式查询数据. A:如果快速查询(从磁盘读数据),hbase是根据rowkey查询的,只要能快速的定位rowkey,  就能实现快速的查询 ...

  8. 学习笔记——抽象工厂模式Abstract Factory

    在工厂模式的基础上,通过为工厂类增加接口,实现其他产品的生产,而不用一类产品就增加一个工厂. 依然以<真菌世界>游戏故事类比,树作为工厂,如果现在有两类树,一类生产快速弄真菌飞机和20毫米 ...

  9. javascript预编译

    刚学前端的小白,第一次写博客,难免有点幼稚.以后每周写两次博客,慢慢积累. 笨鸟不必先飞,但一定是最后一个留下的.加油! JS的预编译定义 在一段程序执行前,js会把var和function这两个关键 ...

  10. ZOJ 2866 Overstaffed Company

    树状数组 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...