PAT甲级专题|树的遍历
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甲级专题|树的遍历的更多相关文章
- PAT甲级专题|链表
PAT链表专题 关于PAT甲级的链表问题,主要内容 就是"建立链表" 所以第一步学会模拟链表,pat又不卡时间,这里用vector + 结构体,更简洁 模拟链表的普遍代码 cons ...
- PAT甲级专题|最短路
PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做, ...
- PAT甲级 1004 树
思路:直接遍历整棵树判定每个结点是否有孩子,没有则把当前高度的叶子节点数加一. AC代码 #include <stdio.h> #include <string.h> #inc ...
- (PAT)L2-006 树的遍历 (二叉树构建)
题目链接:https://www.patest.cn/contests/gplt/L2-006 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格 ...
- pat -1004(树的遍历)
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 思路: (1)用vector记录每 ...
- PAT甲级满分攻略|记一次考试经历
一次考试经历 今天是"大雪",很冷. 来到隔壁的学校考试,记得上一次来河中医是两年前大一刚开学吧,那天晚上印象比较深刻,6个室友骑车到处闲逛.当时还不会Hello world. 很 ...
- PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca
给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...
- PAT甲级题分类汇编——树
本文为PAT甲级分类汇编系列文章. AVL树好难!(其实还好啦~) 我本来想着今天应该做不完树了,没想到电脑里有一份讲义,PPT和源代码都有,就一遍复习一遍抄码了一遍,更没想到的是编译一遍通过,再没想 ...
- pat L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
随机推荐
- (大模拟紫题) Luogu P1953 易语言
原题链接:P1953 易语言 (我最近怎么总在做大模拟大搜索题) 分别处理两种情况. 如果只有一个1或0 直接设一个cnt为这个值,每次输入一个新名字之后把数字替换成cnt,最后cnt++即可. 注意 ...
- MIT线性代数:1.方程组的几何解析
- golang 包依赖管理 godep 使用
介绍: godep是解决包依赖的管理工具,目前最主流的一种,原理是扫描记录版本控制的信息,并在go命令前加壳来做到依赖管理. 1.安装: go get github.com/tools/godep 2 ...
- 腾讯新闻构建高性能的 react 同构直出方案
在腾讯新闻抢金达人活动 node 同构直出渲染方案的总结文章中我们整体了解了下同构直出渲染方案在我们项目中的使用.正如我在上篇文章结尾所说的: 应用型技术的难点不是在克服技术问题,而是在于能够不断的结 ...
- 掌握git命令的正确使用姿势
前言 最近在团队内部发起了一个小的python项目(用tkinter实现一个小工具),但是发现大家对git的使用还不太熟悉,不知道怎么同步代码.解决冲突等等.因为我觉得对测试工程师来说,git应该是必 ...
- JavaScript如何友好的操作的cookie
1.前言 众所周知,在JS中处理cookie有些复杂,因为其操作cookie的接口相当不友好,即BOM的document.cookie属性.这个属性的独特之处在于它会因为使用它的方式不同而表现出不同的 ...
- 学习笔记之vim的使用
很多刚学习linux编程的人总是对vim有一种恐惧,我自己就是这么回事的. 可是当你努力的去尝试学习使用后,才发现它的精髓所在. 在我看来,让vim变得好用的前提是要安装两个插件,ctags和tagl ...
- php [poolwww] seemsbusy (youmayneedto increasepm.start_servers, or pm.min/max_spare_servers)错误解决方法
php [poolwww] seemsbusy (youmayneedto increasepm.start_servers, or pm.min/max_spare_servers)错误解决方法修改 ...
- Webpack 4 Tree Shaking 终极优化指南
几个月前,我的任务是将我们组的 Vue.js 项目构建配置升级到 Webpack 4.我们的主要目标之一是利用 tree-shaking 的优势,即 Webpack 去掉了实际上并没有使用的代码来减少 ...
- MySql——创建数据表,查询数据,排序查询数据
参考资料:<Mysql必知必会> 创建数据表 在学习前首先创建数据表和插入数据.如何安装mysql可以看看上个博客https://www.cnblogs.com/lbhym/p/11675 ...