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 ...
随机推荐
- 【校招面试 之 C/C++】第14题 C++ 内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区(堆栈的区别)
栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清除的变量的存储区.里面的变量通常是局部变量.函数参数等.在一个进程中,位于用户虚拟地址空间顶部的是用户栈,编译器用它来实现函数的调用.和堆一样 ...
- .“代理XP”组件已作为此服务器安全配置的一部分被关闭的解决办法
“代理XP”组件已作为此服务器安全配置的一部分被关闭.系统管理员可以使用sp_configure来启用“代理XP”.有关启用“代理XP”的详细信息,请参阅SQL Server联机丛书中的“外围应用配置 ...
- 4-windows 用cmd 如何输入命令 进入文件夹
比如是你要到d盘的 tmp目录,那么敲入 cd d:\tmp 回车,然后在敲入 d:回车即可
- 1-跑Faster R-CNN项目中的一些问题
原理介绍: https://blog.csdn.net/quincuntial/article/details/79132243 我用的环境: Python 3.5.2 cpu版的ten ...
- 比特币测试网络搭建以及RPC服务开启-配置注意事项
.bitcoin QA Test环境 启动指定参数: "C:\Program Files (x86)\Bitcoin\bitcoin-qt.exe" -testnet -serve ...
- hangfire enqueued but not processing(hangfire 定时任务入队列但不执行)
不生效的方法 //RecurringJob.AddOrUpdate<FamilyAppService>((s) => s.UpdateFamilyLevel(), input.Cro ...
- java的eclipse的使用
1下载eclipse地址:www.eclipse.org/downloads/ 解压就可安装 注意: 这可能你是没有安装java运行环境(jre或jdk) 直接www.java.com,下载就行 下一 ...
- H5笔记周记
9.4 一.介绍浏览器 1.title 主题,题目 2.url 网址 3.body 网站的内容 4.html 语言,超文本标记语言 二.文件介绍 三.标签的结构 (一些快捷键:Ctrl+s 保存 ...
- Windows服务安装、卸载、启动和关闭的管理器
最近在重构公司的系统,把一些需要独立执行.并不需要人为关注的组件转换为Windows服务,Windows服务在使用的过程中有很多好处,相信这一点,就不用我多说了.但是每次都要建立Windows服务项目 ...
- How to use external classes and PHP files in Laravel Controller?
By: Povilas Korop Laravel is an MVC framework with its own folder structure, but sometimes we want t ...