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. Python监控SQL Server数据库服务器磁盘使用情况

    本篇博客总结一下Python采集SQL Server数据库服务器的磁盘使用信息,其实这里也是根据需求不断推进演化的一个历程,我们监控服务器的磁盘走了大概这样一个历程: 1:使用SQL Server作业 ...

  2. 精心整理(含图版)|你要的全拿走!(R数据分析,可视化,生信实战)

    本文首发于“生信补给站”公众号,https://mp.weixin.qq.com/s/ZEjaxDifNATeV8fO4krOIQ更多关于R语言,ggplot2绘图,生信分析的内容,敬请关注小号. 为 ...

  3. 【IOS开发—视图控制器】

    一.UIViewController 视图控制器是UIViewController类或者其子类对象,每个视图控制器都负责管理一个视图层次结构.在UIViewController中有一个重要的UIVie ...

  4. 使用Typescript重构axios(二十七)——添加请求状态码合法性校验

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  5. 大厂面试经:说一下你们线上JVM是如何优化的?

    JVM(Java虚拟机)简单来说就是运行Java代码的解释器,作为螺丝钉程序员JVM其实了解下就差不多啦,不懂JVM内部细节照样能写出优质的代码!但是一到造火箭.飞机的场景(面试)不懂JVM的你,会被 ...

  6. Java自动化测试框架-12 - TestNG之xml文件详解篇 (详细教程)

    1.简介 现在这篇,我们来学习TestNG.xml文件,前面我们已经知道,TestNG就是运行这个文件来执行测试用例的.通过本篇,你可以进一步了解到:这个文件是配置测试用例,测试套件.简单来说,利用这 ...

  7. Python类属性与实例属性理解

    按理讲,类属性改变,类的实例对象这个属性也应该被改变,但是在python中实际却不是这样 class test(): name = 111 a = test() b = test() a.name = ...

  8. nyoj 62-笨小熊(以对应数组中的ASC位 + 1)

    62-笨小熊 内存限制:64MB 时间限制:2000ms Special Judge: No accepted:15 submit:43 题目描述: 笨小熊的词汇量很小,所以每次做英语选择题的时候都很 ...

  9. 【dp】 AreYouBusy

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意: 多组背包, 0类型为为至少去1样, 1为至多取1样, 2 为随意. 如果将2类型 再添加 ...

  10. RHEL7.2 SSH无密码登录非root用户

    1 修改三台虚拟机的/ect/hosts文件 [hadoop@hadoop01 ~]$ cat /etc/hosts 127.0.0.1 localhost localhost.localdomain ...