题目链接: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. Django:之不得不说的web框架们

    python的web框架 Bottle Bpttle是一个快速.简洁.轻量级的基于WSIG的微型web框架,此框架只有一个.py文件,除了python的标准库外,其不依赖任何其它模块. pip ins ...

  2. 查看当前目录每个文件的大小(linux)

    du -sh * 查看当前目录每个文件的大小

  3. angular、bootstrap初稿搭建

    1.bootstrap3.0中,ie8不兼容响应式设计 @media,需要添加如下2个查件 <!-- html5.js for IE less than 9 -->     <!-- ...

  4. 使用SQLCMD在SQLServer执行多个脚本 转载

    出处不明 概述: 作为DBA,经常要用开发人员提供的SQL脚本来更新正式数据库,但是一个比较合理的开发流程,当提交脚本给DBA执行的时候,可能已经有几百个sql文件,并且有执行顺序,如我现在工作的公司 ...

  5. 并查集+bfs+暴力滑窗 Codeforces Round #356 (Div. 2) E

    http://codeforces.com/contest/680/problem/E 题目大意:给你一个n*n的图,然后图上的 . (我们下面都叫做‘点’)表示可以走,X表示不能走,你有如下的操作, ...

  6. android全屏和取消全屏 旋转屏幕

    全屏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 取消全屏 getWindow().clearFlags(Win ...

  7. SQL Server 分页语句

      表中主键必须为标识列,[ID] int IDENTITY (1,1) 1.分页方案一:(利用Not In和SELECT TOP分页) SELECT TOP 页大小 * FROM TestTable ...

  8. java应用测试报告生成(二):利用ant的build.xml生成测试报告

    1.将写好的项目导出 在工程下会生成一个build.xml的蚂蚁图标的文件. 2.右击该文件,选择run as Ant build 其中的测试目录是可以选择的,如果涉及到顺序也可以调整顺序 3.执行后 ...

  9. runtime关联属性示例

    前言 在开发中经常需要给已有的类添加方法和属性,但是Objective-C是不允许给已有类通过分类添加属性的,因为类分类是不会自动生成成员变量的.但是,我们可以通过运行时机制就可以做到了. 本篇文章适 ...

  10. Android图片处理神器BitmapFun源码分析

    作为一名Android开发人员,相信大家对图片OOM的问题已经耳熟能详了,关于图片缓存和解决OOM的开源项目也是相当的多,被大家熟知的就是Universal_image_loader和Volley了, ...