题目链接: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. elasticsearch简介

    elasticsearch 摘要: 1 es是一个分布式全文搜索引擎.特定是:无中心化,实时,扩展性强. 2. es有几个好的概念或者特点:(1)cluster 集群无中心化.(2)shards.分片 ...

  2. Encoded Love-letter

    Encoded Love-letter Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  3. unix文件系统

    转自here 我一向坚持的原则,那就是任何东西的根本性的,本质上的原理以及背后的思想都是及其简单的,所谓的复杂性都是优化与策略化的扩展带来的,正如TCP一样,UNIX的文件系统也不例外!我们必须知道, ...

  4. repeat帮定删除按钮事件,并且生成去人删除提示

    前台 <ItemTemplate> <tr> <td> <asp:LinkButton ID="LinkButton_cancel" On ...

  5. restlet不能接受angular post过来的数据

    修改header create: { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded'} }

  6. Linux入门学习教程:虚拟机体验之KVM篇

    本文中可以学习到的命令: 1. aptitude 是apt-get 不会产生垃圾的版本 2.       dpkg -L virtualbox 显示属于该包的文件 lsmod | grep kvmfi ...

  7. HDU5908 Abelian Period 暴力

    题目大意:将一个数组分成长度为k的几个连续区间,如果每个区间内各个元素出现的次数相同,则称k为一个阿贝尔周期,从小到大打印所有阿贝尔周期,数据间加空格. 题目思路:map+暴力 #include< ...

  8. PHP全选择删除功能

    <script type="text/javascript" language="javascript"> function selectBox(s ...

  9. js框架——angular.js(4)

    1. angular中的对象 其实也不用多说的,前台是可以提取后台定义的对象的—— <body ng-app="MyApp"> <div ng-controlle ...

  10. FZU Problem 1895 整除45问题(整除问题+字符串维护+优化)

    这个题有点烧脑啊,但是只要想清楚被45整除的数,肯定能被5和9整除,能被9整除的数各位加起来肯定是9的倍数,能被5整除的末尾是0或5. 然后dfs的过程稍微不太好懂,还有几个优化必须要注意.dfs的过 ...