PAT甲级专题-树的遍历

涉及知识点:树、建树、深度优先搜索、广度优先搜索、递归

甲级PTA 1004

输出每一层的结点,邻接表vector建树后、用dfs、bfs都可以边搜边存当前层的数据,

#include<bits/stdc++.h>
using namespace std; const int maxn = 110;
int n, m;
vector<int> g[maxn];
int ans[maxn];
int deep = 0; void dfs(int x, int depth) {
if (g[x].size() == 0) {
if (depth > deep) deep = depth;
ans[depth]++;
return;
}
for (int i = 0; i < g[x].size(); i++) {
dfs(g[x][i], depth + 1);
}
} int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int id,k;
cin >> id;
cin >> k;
for (int j = 1; j <= k; j++) {
int id2;
cin >> id2;
g[id].push_back(id2);
}
}
dfs(1, 0);
for (int i = 0; i <= deep; i++) {
if (i != deep) cout << ans[i] << " ";
else cout << ans[i];
}
return 0;
}

甲级PTA 1020

中序、后序序列,找出层次遍历的序列

前置知识,中序后序序列来建树、中序后序序列找出前序序列

//中序 后序  找 前序
/*
void build(int root, int start, int end) {
if (start > end) return;
if (root < 1) return;
int pos = start;
while (pos < end && iorder[pos] != porder[root]) pos++;
pre[++idx] = porder[root];
//cout << porder[root] << endl;
build(root - (end - pos + 1), start, pos - 1);
build(root - 1, pos + 1, end);
}
*/

本题代码


#include<bits/stdc++.h>
using namespace std; const int maxn = 50;
int n;
int porder[maxn];
int iorder[maxn];
int a[maxn];
int idx = 0;
int deep = 0;
vector<int> v[maxn];
int ans[maxn]; //后序 中序 在递归时 使用vector存下当前一层的数,因为按左子树优先递归,所以存的序列符合题意
void build(int root, int start, int end, int depth) {
if (start > end) return;
int pos = start;
while (pos < end && iorder[pos] != porder[root]) pos++;
v[depth].push_back(porder[root]);
if (depth > deep) deep = depth;
//cout << "depth = " << depth << " " << porder[root] << endl;
build(root - (end - pos + 1), start, pos - 1, depth + 1);
build(root - 1, pos + 1, end, depth + 1);
} int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> porder[i];
for (int i = 1; i <= n; i++) cin >> iorder[i];
//build(n, 1, n, 1, n);
//build(n, 1, n);
build(n, 1, n, 1);
int num = 1;
for (int i = 1; i <= deep; i++) {
for (int j = 0; j < v[i].size(); j++) {
ans[num++] = v[i][j];
}
}
for (int i = 1; i <= n; i++) {
if (i == n) cout << ans[i];
else cout << ans[i] << " ";
}
return 0;
}

甲级PTA 1053

找出路径长度等于 S 的路径,按结点权值的字典序排列

vector按权值大的优先存树的边、dfs搜索边搜边存答案。

#include<bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn = 1010;
vector<int> g[maxn];
int w[maxn];
int n, m;
ll s;
int path[maxn];
vector<int> ans[maxn];
int len = 0; struct node {
int v;
}; void dfs(int x, int depth) {
path[depth] = w[x];
if (g[x].size() == 0) {
ll temp = 0;
for (int i = 0; i <= depth; i++) temp += path[i];
if (temp == s) {
for (int i = 0; i <= depth; i++) ans[len].push_back(path[i]);
len++;
}
return;
}
for (int i = 0; i < g[x].size(); i++) {
dfs(g[x][i], depth + 1);
}
} bool cmp(int a,int b) {
return w[a] > w[b];
} node temp[1010];
int main() {
cin >> n >> m >> s;
for (int i = 0; i < n; i++) cin >> w[i];
for (int i = 1; i <= m; i++) {
int id1, k, id2;
cin >> id1 >> k;
for (int j = 1; j <= k; j++) {
cin >> id2;
g[id1].push_back(id2);
}
}
for (int i = 0; i < n; i++) sort(g[i].begin(), g[i].end(), cmp); dfs(0, 0); for (int i = 0; i < len; i++) {
for (int j = 0; j < ans[i].size(); j++) {
if (j == ans[i].size() - 1) cout << ans[i][j] << endl;
else cout << ans[i][j] << " ";
}
} return 0;
}

甲级PTA 1079

算出,从各个叶节点 到 根的距深度(dfs),按题意计算答案。

#include<bits/stdc++.h>
using namespace std; const int maxn = 1e5+10;
int n;
double r, p,ans = 0;
vector<int> g[maxn];
double dat[maxn]; void dfs(int x, int depth) {
if (g[x].size() == 0) {
ans += (dat[x] * pow(1 + r, depth));
return;
}
for (int i = 0; i < g[x].size(); i++) {
dfs(g[x][i], depth + 1);
}
} int main() {
cin >> n >> p >> r;
r = r * 0.01;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
int id;
double val;
if (k == 0) {
cin >> val;
dat[i] = val;
continue;
}
for (int j = 0; j < k; j++) {
cin >> id;
g[i].push_back(id);
}
}
dfs(0, 0);
printf("%.1f", ans * p);
return 0;
}

甲级PTA 1086

先序、中序序列,找出后序序列

我的做法:先根据先序中序序列建树、再后序遍历找出后序序列。

先序确定根,中序划分左右子树

但是这题题目没说完善,万一有重复元素,应该就建不了树了

#include<bits/stdc++.h>
using namespace std; const int maxn = 50;
int n;
struct node {
int v;
node * l, * r;
}; int pre[maxn];
int in[maxn];
int post[maxn];
int idx1 = 0, idx2 = 0,idx = 0;
stack<int> st; node * build(int root,int il,int ir) {
if (il > ir) return NULL;
int pos = il;
while (pos <= ir && in[pos] != pre[root]) pos++;
node* Root = new node;
Root->v = pre[root];
Root->l = build(root+1, il, pos - 1);
Root->r = build(root+(pos-il)+1 ,pos+1,ir);
return Root;
} void postOrder(node *Root) {
if (Root == NULL) return;
if (Root->l) postOrder(Root->l);
if (Root->r) postOrder(Root->r);
post[++idx] = Root->v;
} int main() {
cin >> n;
while (1) {
string ins;
int id;
cin >> ins;
if (ins == "Push") {
cin >> id;
pre[++idx1] = id;
st.push(id);
}else {
int top = st.top();
in[++idx2] = top;
st.pop();
}
if (idx1 == n && idx2 == n) break;
}
node* Root = new node;
Root = build(1, 1, n);
postOrder(Root);
for (int i = 1; i <= n; i++) {
if (i == n) cout << post[i];
else cout << post[i] << " ";
} return 0;
}

PAT甲级专题|树的遍历的更多相关文章

  1. PAT甲级专题|链表

    PAT链表专题 关于PAT甲级的链表问题,主要内容 就是"建立链表" 所以第一步学会模拟链表,pat又不卡时间,这里用vector + 结构体,更简洁 模拟链表的普遍代码 cons ...

  2. PAT甲级专题|最短路

    PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做, ...

  3. PAT甲级 1004 树

    思路:直接遍历整棵树判定每个结点是否有孩子,没有则把当前高度的叶子节点数加一. AC代码 #include <stdio.h> #include <string.h> #inc ...

  4. (PAT)L2-006 树的遍历 (二叉树构建)

    题目链接:https://www.patest.cn/contests/gplt/L2-006 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格 ...

  5. pat -1004(树的遍历)

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 思路: (1)用vector记录每 ...

  6. PAT甲级满分攻略|记一次考试经历

    一次考试经历 今天是"大雪",很冷. 来到隔壁的学校考试,记得上一次来河中医是两年前大一刚开学吧,那天晚上印象比较深刻,6个室友骑车到处闲逛.当时还不会Hello world. 很 ...

  7. PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca

    给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...

  8. PAT甲级题分类汇编——树

    本文为PAT甲级分类汇编系列文章. AVL树好难!(其实还好啦~) 我本来想着今天应该做不完树了,没想到电脑里有一份讲义,PPT和源代码都有,就一遍复习一遍抄码了一遍,更没想到的是编译一遍通过,再没想 ...

  9. pat L2-006. 树的遍历

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

随机推荐

  1. MySQL系统变量auto_increment_increment与auto_increment_offset学习总结

    在MySQL中,系统变量auto_increment_increment与auto_increment_offset是与自增列相关的两个参数变量.在官方文档中,将其划分为Replication Mas ...

  2. Java IO编程——文件拷贝

    在操作系统里面有一个copy命令,这个命令的主要功能是可以实现文件的拷贝处理,现在要求模拟这个命令,通过初始化参数输入拷贝的源文件路径与拷贝的目标路径实现文件的拷贝处理. 需求分析: ·需要实现文件的 ...

  3. 非旋treap (fhq treap) 指针版

    传送门 看了一圈,好像真的没什么用指针的呢.. 明明觉得指针很好看(什么??你说RE???听不见听不见) 其实我觉得用数组的话不RE直接WA调起来不是更困难嘛,毕竟通过gdb还可以知道哪里RE,WA就 ...

  4. MySQL 语句执行过程详解

    MySQL 原理篇 MySQL 索引机制 MySQL 体系结构及存储引擎 MySQL 语句执行过程详解 MySQL 执行计划详解 MySQL InnoDB 缓冲池 MySQL InnoDB 事务 My ...

  5. docker基本操作教程

    镜像操作 获取镜像 从Docker Hub搜索镜像: docker search ubuntu 下载镜像: docker pull ubuntu:18.04 若下载镜像速度较慢,更改镜像源: Ubun ...

  6. es ik 分词 5.x后,设置默认分词

    1.使用模板方式,设置默认分词 注: 设置模板,需要重新导入数据,才生效 通过模板设置全局默认分词器 curl -XDELETE http://localhost:9200/_template/rtf ...

  7. linux 安装swoole扩展方法

    linux 安装swoole扩展方法 wget https://github.com/swoole/swoole-src/archive/v1.9.23.tar.gz接下去就不说了 说明下 下载swo ...

  8. nsq (三) 消息传输的可靠性和持久化[二]diskqueue

    上一篇主要说了一下nsq是如何保证消息被消费端成功消费,大概提了一下消息的持久化,--mem-queue-size 设置为 0,所有的消息将会存储到磁盘. 总有人说nsq的持久化问题,消除疑虑的方法就 ...

  9. ZeroC ICE中的对象模型和概念

    Ice对象的模型和概念. Ice Object并非是我们的接口实现类的实例对象.我们的接口实现类的实例对象只是充当Ice Object的Servant的角色.一个Ice Object可以有众多Serv ...

  10. ZeroC ICE中的对象

    在ZeroC Ice中定义了三种基本对象类型. 它们分别是IceProxy::Ice::Object(于Ice/Proxy.h),Ice::Object(于Ice/Object.h)和Ice::Loc ...