![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
pat
池塘春草梦
未觉池塘春草梦,阶前悟叶已秋声。
展开
-
1155 Heap Paths 分数 30
PAT题目目录判断是非为堆,进一步判断是否为大根堆、小根堆。1用数组存储这个完全二叉树;2遍历,找出所有从根节点到叶节点的路径,同时用(bool) gt lt记录是否存在当前节点比前一个结点大或小的情况;3根据gt lt的状态判断是否为堆、大根堆、小根堆。...原创 2022-06-19 18:41:17 · 190 阅读 · 1 评论 -
1106 Lowest Price in Supply Chain 分数 25
供应链最低价格,即求树的最短距离,采用记忆搜索、dfs深度优先的方法。原创 2022-06-14 20:38:09 · 93 阅读 · 0 评论 -
1090 Highest Price in Supply Chain 分数 25
题目链接#include <iostream>#include <cstring>#include <cmath>using namespace std;const int N=1e5+10;int n;double P,R;int p[N],f[N];int dfs(int u){ if(f[u]!=-1) return f[u]; if(p[u]==-1) return f[u]=0; return f[u]=dfs(p[u]原创 2022-05-10 21:24:50 · 102 阅读 · 0 评论 -
1079 Total Sales of Supply Chain 分数 25
题目链接#include <iostream>#include <algorithm>#include <cmath>#include <cstring>using namespace std;const int N=100010;double P,R;int n;int p[N],f[N];//父节点,距离int cnt[N];//子节点的营业额int dfs(int u){ if(f[u]!=-1) return f[u];原创 2022-05-10 20:46:34 · 77 阅读 · 0 评论 -
1094 The Largest Generation 分数 25
PAT题目目录#include <iostream>#include <vector>using namespace std;const int N=110;bool g[N][N];int n,m;vector<int> level[N];int main(){ cin>>n>>m; for(int i=1;i<=m;i++){ int id,k;cin>>id>>k;原创 2022-05-06 21:17:43 · 85 阅读 · 0 评论 -
1053 Path of Equal Weight 分数 30
PAT题目目录#include <iostream>#include <algorithm>#include <cstring>#include <vector>using namespace std;const int N=1e4+10;int n,m,sum;bool g[N][N];int w[N];vector< vector<int> >ans;void dfs(int u,int s,vector<原创 2022-05-06 19:44:59 · 70 阅读 · 0 评论 -
1135 Is It A Red-Black Tree 分数 30
PAT题目目录#include <iostream>#include <algorithm>#include <unordered_map>using namespace std;unordered_map<int,int> pos;bool ans;const int N=40;int pre[N],in[N];int build(int il,int ir,int pl,int pr,int &sum){ int root原创 2022-04-27 21:16:30 · 165 阅读 · 0 评论 -
1123 Is It a Complete AVL Tree 分数 30
PAT题目目录#include <iostream>#include <algorithm>using namespace std;const int N=30;int n;int l[N],r[N],v[N],h[N],idx=0;int q[N],pos[N];void update(int u){ h[u]=max(h[l[u]],h[r[u]])+1;}int get_balance(int u){ return h[l[u]]-h[r[u原创 2022-04-26 20:09:38 · 69 阅读 · 0 评论 -
1066 Root of AVL Tree 分数 25
PAT题目链接#include <iostream>#include <algorithm>using namespace std;int n;const int N=30;int l[N],r[N],v[N],h[N],idx=0;//l[编号]=左儿子编号,v[编号]=权值,h[编号]=高度int get_balance(int u){ return h[l[u]]-h[r[u]];}void update(int u){ h[u]=max(h[原创 2022-04-25 21:10:34 · 165 阅读 · 0 评论 -
1138 Postorder Traversal (25 分)
PAT题目链接#include <iostream>#include <unordered_map>using namespace std;const int N=5e4+10;int n,post;//post记录后序遍历的第一个int pre[N],in[N];//前序和中序遍历unordered_map <int,int> post_in;void built(int il,int ir,int pl,int pr){ int root=pr原创 2022-04-19 21:21:14 · 86 阅读 · 0 评论 -
1127 ZigZagging on a Tree (30 分)
PAT题目链接Z字形遍历二叉树#include <iostream>#include <algorithm>#include <unordered_map>using namespace std;const int N=35;int n;int in[N],post[N],q[N];unordered_map <int,int> l,r,pos;int built(int il,int ir,int pl,int pr){ //参数原创 2022-04-12 20:53:16 · 60 阅读 · 0 评论 -
1119 Pre- and Post-order Traversals (30 分)
PAT目录#include <iostream>using namespace std;const int N=35;int n;int pre[N],post[N];int dfs(int l1,int r1,int l2,int r2,string &in){ if(l1>r1) return 1; if(pre[l1]!=post[r2]) return 0; int cnt; for(int i=0;i<=r1;i++){原创 2022-04-08 20:55:49 · 57 阅读 · 0 评论 -
1115 Counting Nodes in a BST (30 分)
PAT目录#include <iostream>using namespace std;const int N=1010;int n;int l[N],r[N],w[N],idx=0;int cnt[N],max_depth=0;void insert(int &u,int ww){ //u前面要加&,例如开始root=0,加入25之后root=1(记录编号),主函数当中的root应该也随着变化 if(!u) u=++idx,w[u]=ww;原创 2022-04-06 19:05:50 · 53 阅读 · 0 评论 -
1110 Complete Binary Tree (25 分)
PAT目录#include <iostream>#include <cstring>using namespace std;int n;const int N=25;int l[N],r[N];int maxk,maxid;bool hasfather[N];void dfs(int u,int k){ if(u==-1) return; if(k>maxk) maxk=k,maxid=u; if(l[u]!=-1) dfs(l[u],原创 2022-04-01 21:20:13 · 54 阅读 · 0 评论 -
1102 Invert a Binary Tree (25 分)
#include <iostream>#include <algorithm>#include <cstring>using namespace std;int n;const int N=15;int l[N],r[N],q[N];bool hasfather[N];void traversal(int root){ if(root==-1) return; traversal(l[root]); traversal(r[root原创 2022-04-01 20:35:24 · 232 阅读 · 0 评论 -
1086 Tree Traversals Again (25 分)
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(原创 2022-03-30 20:51:59 · 78 阅读 · 0 评论 -
1064 Complete Binary Search Tree (30 分)
#include <iostream>#include <algorithm>using namespace std;const int N=1010;int n;int w[N],tr[N];//w存放的是输入的权值。tr存放的是树的层次遍历,也就是完全二叉树的数组存储void inorder(int u,int &k){ if((u<<1)<=n) inorder(u<<1,k); tr[u]=w[k++];原创 2022-03-29 16:43:47 · 284 阅读 · 0 评论 -
1043 Is It a Binary Search Tree (25 分)
#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int N=1010;int n,cnt;//后序遍历用cntint preorder[N],inorder[N],postorder[N];bool built(int il,int ir,int pl,int pr,int type){ if(il>ir) return tru原创 2022-03-28 21:24:44 · 53 阅读 · 0 评论 -
1099 Build A Binary Search Tree (30 分)
#include <iostream>#include <algorithm>using namespace std;const int N=110;int n;int l[N],r[N];//存放左右子树的序号int q[N];int a[N],w[N];void inorder(int u,int &k){//u为序号 if(u==-1) return; inorder(l[u],k); w[u]=a[k++]; ino原创 2022-03-28 21:21:54 · 248 阅读 · 0 评论 -
PAT甲级题目目录
树1004 Counting Leaves1020 Tree Traversals原创 2022-03-11 16:10:09 · 258 阅读 · 0 评论 -
1021 Deepest Root (25 分)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.Input Specification:Each in原创 2022-03-11 16:07:42 · 190 阅读 · 0 评论 -
PAT1004 Counting Leaves (30 分)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.Input Specification:Each input file contains one test case. Each case starts with a line containing 0<N<100, the number of nodes原创 2022-03-11 10:29:52 · 149 阅读 · 0 评论 -
PAT1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.**Input SpecificatEach input fi原创 2022-03-11 10:24:30 · 191 阅读 · 0 评论