题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; pair<]; ]; ; ]; void dfs(int x){ ) return ; dfs(a[x].first); ans[x]=b[++c…
1043 Is It a Binary Search Tree (25 分)   A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a…
题意: 输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树.输出最底层和最底层上一层的结点个数之和,例如x+y=x+y. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; typedef struct Node{ int value; int vis; int level; int visl,…
题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该城市所能获得的快乐,接着输入M行每行包括一条道路的两端城市名称和道路的长度.输出从起点城市到目标城市"ROM"获得最大快乐且经历道路长度最短的道路条数和经历的道路长度和获得的快乐总数以及其中经过城市最少的那条路的到达每个城市所获得的平均快乐.下一行输出这条路的路径,城市名之间用"…
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的所有的城市的名称都是字符串(三个大写字母),所以我先将每个字符串计算一个hash值去代表每一个城市,接着就是用dijkstra算法去逐步判断各种情况,是直接用了一个dijkstra去计算,而没有说是先dijkstra算出最短距离再dfs所有的路径去找出最优解,因为我觉得这题局部最优就能反映到全局最优,也不知是…
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了旋转规则. 现在给出一系列插入, 您应该输出生成的AVL树的级别遍历序列,并告知它是否是完整的二叉树. 输入规格: 每个输入文件包含一个测试用例.对于每种情况,第一行包含正整数N(<= 20). 一行中的所有数字都以空格分隔. 输出规格: 对于每个测试用例,将键逐个插入到初始空的AVL树中.然后首先…
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtre…
链接:1135. Is It A Red-Black Tree (30) 红黑树的性质: (1) Every node is either red or black. (2) The root is black. (3) Every leaf (NULL) is black. (4) If a node is red, then both its children are black. (5) For each node, all simple paths from the node to de…
1135. Is It A Red-Black Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties: (1) Every node is eith…
我先在CSDN上面发表了同样的文章,见https://blog.csdn.net/weixin_44385565/article/details/88863693 排版比博客园要好一些.. 1135 Is It A Red-Black Tree (30 分) There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 proper…
https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties: (1) Every node is either red or black. (2) The…
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 题意: 给定一棵二叉搜索树的先序遍历结果,问这棵树是不是一棵红黑树. 思路: 首先需要明确二叉搜索树和红黑树的性质. 二叉搜索树的每个节点,左子树上的值都比这个节点的值小,右子树上的值都比这个节点的值大. 因此对于一棵二叉搜索树,如果给定了先序遍历结果,就可以唯一确定这棵树了. 红黑树的性质: 1.每个节点是红色或是黑色 2.根节点是黑…
题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.Given any two nodes in a binary tree, you are supposed to find their LCA. 最小共同祖先(LCA)是一棵树中两个节点U和V最深的那个公共父节点.要求给一棵树,以及两个节点,请你…
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL树,按照层序遍历输出,最后判断这棵AVL树是不是完全二叉树. 思路: 这道题过段时间还要再来手搓一发.AVL模板要记住. 判断是不是完全二叉树的话只用看,如果有一个节点儿子是空,而他之后又出现了至少有一个儿子的节点的话,就不是完全二叉树.[蛮巧妙的] #include<cstdio> #inclu…
https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than th…
1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 a~N1…
嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by…
1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a…
1033 To Fill or Not to Fill (25 分)   With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may…
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illust…
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than or equal to the node's key. The right subtree of a node contains only nodes with…
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness. Input Specification: Each input file contains one test case. For each case, th…
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greate…
题意: 输入一个正整数N(<=10000),接着依次输入N个学生的ID.输入一个正整数Q,接着询问Q次,每次输入一个学生的ID,如果这个学生的ID不出现在之前的排行榜上输出Are you kidding,否则如果已经询问过输出Checked,否则如果这位学生排名第一输出Mystery Award,否则如果这位学生的排名是质数输出Minion,否则输出Chocolate. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/…
题意:输入一个正整数N(<=1e5),两个小数P和R,分别表示树的结点个数和商品原价以及每下探一层会涨幅的百分比.输出叶子结点深度最小的商品价格和深度最小的叶子结点个数. trick: 测试点1只有根节点一个点,输出P和1. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; vector<]; ]; void dfs(int x,int y){ )…
题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点组成的链表). AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],val[]; vector<pair<int,int> >ans,a…
题意: 输入一个正整数N(<=1e5),和两个小数r和f,表示树的结点总数和商品的原价以及每向下一层价格升高的幅度.下一行输入N个结点的父结点,-1表示为根节点.输出最深的叶子结点处购买商品的价格以及有几个深度最深的结点. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; vector<]; int mx; ]; void dfs(int x…
题意: 输入一个正整数N(<=1e5),表示共有N个结点,接着输入两个浮点数分别表示商品的进货价和每经过一层会增加的价格百分比.接着输入N行每行包括一个非负整数X,如果X为0则表明该结点为叶子结点接着输入一个整数表示该零售商进货的数量,X不为零则接着输入X个正整数表示它的下级经销商是哪些结点.输出所有零售商进货的总价.(结点从0~N-1,0为根节点即供应商) AAAAAccepted code: #include<bits/stdc++.h> using namespace std; ]…
题意: 输入一个四位的正整数N,输出每位数字降序排序后的四位数字减去升序排序后的四位数字等于的四位数字,如果数字全部相同或者结果为6174(黑洞循环数字)则停止. trick: 这道题一反常态的输入的数字是一个int类型而不是包含前导零往常采用字符串的形式输入,所以在测试点2,3,4如果用字符串输入会超时..... AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace st…
题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],b[]; int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(…