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. 树的遍历的更多相关文章
- GPTL—练习集—006树的遍历
#include<bits/stdc++.h> using namespace std; typedef int daTp;//datatype typedef struct BTNode ...
- 天梯 L2 树的遍历(已知后序中序求层序)
树的遍历 (25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数.第二行 ...
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- javascript实现数据结构: 树和二叉树的应用--最优二叉树(赫夫曼树),回溯法与树的遍历--求集合幂集及八皇后问题
赫夫曼树及其应用 赫夫曼(Huffman)树又称最优树,是一类带权路径长度最短的树,有着广泛的应用. 最优二叉树(Huffman树) 1 基本概念 ① 结点路径:从树中一个结点到另一个结点的之间的分支 ...
- PTA 7-10 树的遍历(二叉树基础、层序遍历、STL初体验之queue)
7-10 树的遍历(25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数 ...
- 数据结构--树(遍历,红黑,B树)
平时接触树还比较少,写一篇博文来积累一下树的相关知识. 很早之前在数据结构里面学的树的遍历. 前序遍历:根节点->左子树->右子树 中序遍历:左子树->根节点->右子树 后序遍 ...
- YTU 3023: 树的遍历
原文链接:https://www.dreamwings.cn/ytu3023/2617.html 3023: 树的遍历 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: 2 题 ...
- 团体程序设计天梯赛-练习集L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- leetcode404-----简单的树的遍历
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- pat L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
随机推荐
- 错误: libstdc++.so.6: cannot open shared object file: No such file or directory
解压完别人提供的openwrt代码,编译时,出现如下错误: # configuration written to .config#mips-openwrt-linux-uclibc-gcc: erro ...
- LD_LIBRARY_PATH vs LIBRARY_PATH
LIBRARY_PATH is used by gcc before compilation to search for directories containing libraries that n ...
- split a string in C++
This is my favourite way to iterate through a string. You can do what you want per word. string line ...
- android 瀑布流效果(仿蘑菇街)
我们还是来看一款示例:(蘑菇街) 看起来很像我们的gridview吧,不过又不像,因为item大小不固定的,看起来是不是别有一番风味,确实如此.就如我们的方角图形,斯通见惯后也就出 ...
- SUPERVISOR进程管理器配置指南
SUPERVISOR进程管理器配置指南1. supervisor简介1.1. 官网http://supervisord.org/ 1.2. 介绍Supervisor是一个进程控制系统. 它是一个C/S ...
- java 单例模式及getInstance的好处
1.什么是单例模式 简单理解为,有一个类,只能有一个实例化对象,这就是单例模式. 2.getInstance的好处 首先看一下怎样使用getInstance实现单例模式 public class Co ...
- zf-关于交换工具配置文件,交换的“列名字段”前面加个“0,1,2”的意思。
- C++ 字符串字面值
C++ 字符串字面值 C++ 基本字符串类型 C++ 字符串类型 char 和 wchar_t c11 新增了 char16_t 和 char32_t 例子: wchat_t title[] = L& ...
- The Rings Akhaten
在其他的平行宇宙中存在着一个古老的星系--Akhaten,星系中有七个世界,上面生活着Panbabylonian.Lucanian等物种,不过外界也常常把他们统称为Akhet,因为这七个世界环绕着同一 ...
- php 常用的JS
function doit(){ var sel_val=$("#type").val(); if (sel_val=='') { $("#bigClassId1&q ...