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. 《Effective Java》 读书笔记(六)避免创建不必要的对象

    java 有很多修饰类的属性的关键字:常用的static,final 说说final和static吧,平时在编程的时候,这两个关键字很多时候都觉得可有可无,最多的时候就是他们俩同时出现----定义常量 ...

  2. 学习笔记12JS异步请求

    *一般用JS来监听按钮事件,都应该先监听页面OnLoad事件. *Js写在哪里,就会在页面解析到哪里执行. 异步请求:所谓异步请求,就是使用JS来监听按钮点击事件,并且发送请求,等到回复后,再使用JS ...

  3. Spring Cloud Gateway使用简介

    Spring Cloud Gateway是类似Nginx的网关路由代理,有替代原来Spring cloud zuul之意: Spring 5 推出了自己的Spring Cloud Gateway,支持 ...

  4. 易初大数据 spss 2019年10月31日 wangqingchao

    ---恢复内容开始--- 1.描述性统计分析方法是指应用分类.制表.图形及概括性数据指标来概括数据分析特征的方法. 2.而推断性统计分析方法则是通过随机抽样,应用统计方法把从样本数据得到的结论推广到总 ...

  5. C语言I博客作业08

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 作业 我在这个课程的目标是 熟悉使用while和do-while结构解决问题 这个作业在那个具体方面帮助我实现目标 pta题目及查找的资料 ...

  6. Uber Go 语言编码规范

    Uber Go 语言编码规范 Uber 是一家美国硅谷的科技公司,也是 Go 语言的早期 adopter.其开源了很多 golang 项目,诸如被 Gopher 圈熟知的 zap.jaeger 等.2 ...

  7. sqlite修改表、表字段等与sql server的不同之处

    sqlite中只支持 ALTER TABLE 命令的 RENAME TABLE 和 ADD COLUMN. 其他类型的 ALTER TABLE 操作如 DROP COLUMN,ALTER COLUMN ...

  8. mybatis什么时候必须指定jdbcType

    #{property,javaType=int,jdbcType=NUMERIC}如果一个列允许 null 值,并且会传递值 null 的参数,就必须要指定 JDBC Type

  9. PHP 核心特性 - 命名空间

    提出 在命名空间提出之前,不同的组件很容易碰到命名的冲突,例如 Request .Response 等常见的命名.PHP 在 5.3 后提出了命名空间用来解决组件之间的命名冲突问题,主要参考了文件系统 ...

  10. nyoj 10 skiing (DFS)

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...