pat树之专题(30分)
(好好复习是王道)
1115. Counting Nodes in a BST (30)
分析:简单题——将bst树构造出来,然后给每个节点打上高度。最后求出树的高度。然后count树高的节点数加上树高节点数-1就ok了
#include <iostream>
#include <cstdio> #define MAXN 1003
using namespace std;
typedef struct Node{
int key,hight;
int left,right;
}Node; Node node[MAXN];
int solve_hight[MAXN];
int main()
{
int n,index1,index2,max,a,b;
scanf("%d",&n); scanf("%d",&node[].key);
node[].left=node[].right=-;
for(int i=;i<n;i++){
scanf("%d",&node[i].key);
node[i].left=node[i].right=-;
int j=;
while(){
if(node[i].key>node[j].key)
if(node[j].right!=-)
j=node[j].right;
else
{
node[j].right=i;
break;
}
else
if(node[j].left!=-)
j=node[j].left;
else
{
node[j].left=i;
break;
}
}
}
index1=index2=;
solve_hight[index2++]=;
node[].hight=;
while(index1!=index2){
if(node[index1].left!=-){
solve_hight[index2++]=node[index1].left;
node[node[index1].left].hight=node[index1].hight+;
}
if(node[index1].right!=-){
solve_hight[index2++]=node[index1].right;
node[node[index1].right].hight=node[index1].hight+;
}
index1++;
} a=b=max=;
for(int i=;i<n;i++){
if(node[i].left==-&&node[i].right==-&&node[i].hight>max)max=node[i].hight;
}
for(int i=;i<n;i++){
if(max==node[i].hight)a++;
if(max-==node[i].hight)b++;
}
printf("%d + %d = %d\n",a,b,a+b); return ;
}
1099. Build A Binary Search Tree (30)
类似于上题的简单题,看来pat的题目是一年年变难,难度都被刷上去了。先构建树的框架,然后将所给序列中序遍历,最后层次遍历给出结果。每个人都要找到最适合自己,能最快写出的树的层次遍历的代码。
#include <iostream>
#include <cstdio>
#include <algorithm>
#define MAXN 105
using namespace std; typedef struct Node{
int left,right;
Node(){
left=right=-;
}
}Node;
Node node[MAXN];
int keynum[MAXN],ans[MAXN],mid[MAXN],count_num;
void mid_travel(int index){
if(node[index].left!=-)
mid_travel(node[index].left); mid[count_num++]=index;
if(node[index].right!=-)
mid_travel(node[index].right); }
int main()
{
int n,index1,index2;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d%d",&node[i].left,&node[i].right);
index1=index2=;
ans[index2++]=; while(index1!=index2){
if(node[ans[index1]].left!=-)
ans[index2++]=node[ans[index1]].left;
if(node[ans[index1]].right!=-)
ans[index2++]=node[ans[index1]].right;
index1++;
}
for(int i=;i<n;i++){
scanf("%d",&keynum[i]);
}
//得到中序遍历序列
sort(keynum,keynum+n);
count_num=;
mid_travel();
bool flag=false;
for(int i=;i<n;i++){
for(int j=;j<n;j++)
if(ans[i]==mid[j]){
if(!flag){
printf("%d",keynum[j]);
flag=true;
}else
printf(" %d",keynum[j]);
break;
}
}
return ;
}
pat树之专题(30分)的更多相关文章
- PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
- [PAT] 1147 Heaps(30 分)
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)
1147 Heaps (30 分) In computer science, a heap is a specialized tree-based data structure that sati ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT A1147 Heaps (30 分)——完全二叉树,层序遍历,后序遍历
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT 垃圾箱分布(30分)dijstra
垃圾箱分布 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁都不愿意守着垃圾 ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- [PAT] 1143 Lowest Common Ancestor(30 分)
1143 Lowest Common Ancestor(30 分)The lowest common ancestor (LCA) of two nodes U and V in a tree is ...
- PAT甲级——1135 Is It A Red-Black Tree (30 分)
我先在CSDN上面发表了同样的文章,见https://blog.csdn.net/weixin_44385565/article/details/88863693 排版比博客园要好一些.. 1135 ...
随机推荐
- ubuntu下tomcat的安装及注册成系统服务
在ubuntu下tomcat的安装有两种方式,第一种是下载二进制文件,解压安装:第二种则是使用apt-get自动下载.这里不推荐第二种方法安装,因为这种方法安装会像天女散花一样把安装的文件散落在系统的 ...
- Validate常用校验
1.首先将jQuery.js和jquery.validate.js加入对应的页面中,如果要中文的提示语还要把messages_zh.js加入,以及对应的css文件. <link href=&qu ...
- springboot 日志2
SpringBoot关于日志的官方文档 1.简述 SpringBoot官方文档关于日志的整体说明 本博客基于SpringBoot_1.3.6大家请先简单看下这篇英文的官方文档,文中有说 Sprin ...
- (四)创建ROS程序包(就是软件包)
你的 ROS 程序包都放到下面这个目录里, 切换到这个目录: $ cd ~/catkin_ws/src 使用下面的命令: 创建一个 ROS 程序包 名字就叫:beginner_tutorials $ ...
- YII2中日志的配置与使用
YII2中给我们提供了非常方便的日志组件,只需要简单配置一下就可以使用. 我们在config/web.php中配置如下: return [ //log必须在bootstrap期间就被加载,便于及时调度 ...
- [z]libevent入门教程:Echo Server based on libevent 不指定
[z]https://www.felix021.com/blog/read.php?2068 花了两天的时间在libevent上,想总结下,就以写简单tutorial的方式吧,貌似没有一篇简单的说明, ...
- Django ORM那些相关操作zi
Django ORM那些相关操作 一般操作 看专业的官网文档,做专业的程序员! 必知必会13条 <1> all(): 查询所有结果 <2> filter(**kwargs) ...
- .Net连接字符串设置连接池大小显著提高数据库速度
在访问mysql数据库时,如果在连接字符串中设置使用连接池,同时设置连接池大小,经测试,可以显著提高访问数据库时的速度. 连接字符串: connectionStrings> <add ...
- Django TypeError: isinstance() arg 2 must be a type or tuple of types
报错: TypeError: isinstance() arg must be a type or tuple of types from django.db import modelsfrom dj ...
- Linux操作系统-系统安装与分区
.磁盘分区 使用分区工具在磁盘上划分几个逻辑部分,一旦分成几个分区,不同类型的目录和文件可以存储进不同的分区2.分区类型主分区:最多只能有4个扩展分区:最多只能有1个:主分区加扩展分区最多有4个:扩展 ...