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 ...
随机推荐
- 使用python读写CSV文件
# -*- coding:UTF-8 -*- __autor__ = 'zhouli' __date__ = '2018/10/25 21:14' import csv with open('resu ...
- [leetcode]692. Top K Frequent Words K个最常见单词
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...
- ios 获取当前时间
1.第一种返回的时间是一个整个的字符串. NSDate *timeDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateForm ...
- 有关Oracle统计信息的知识点[z]
https://www.cnblogs.com/sunmengbbm/p/5775211.html 一.什么是统计信息 统计信息主要是描述数据库中表,索引的大小,规模,数据分布状况等的一类信息.例如, ...
- python 网络基础 之 网络协议
osi 七层协议 互联网协议按照功能不同分为osi七层或者tcp/ip 五层或tcp/ip 四层 tcp/ip 四层 1.应用层 2.传输层 3.网络层 4.网络接口层 tcp/ip 五层 1.应用层 ...
- JS设置CSS样式的集中方式
1. 直接设置style的属性 某些情况用这个设置 !important值无效 如果属性有'-'号,就写成驼峰的形式(如textAlign) 如果想保留 - 号,就中括号的形式 element. ...
- Linux_(1)基本命令(上)
一.基本命令1.我是谁 whoami --who am i2.谁在线 who w3.显示当前路径(定位) pwd4.切换目录 cd ~返回主目录 cd ..返回上一级目录5.查看某个目录中的子目录和文 ...
- C++中的fstream,ifstream,oftream
https://blog.csdn.net/kingstar158/article/details/6859379 先mark一个大佬的随笔,有时间再回头看 总结: 使用ifstream和ofstre ...
- web框架--MVC、MTV
一.MVC框架: MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式:[1] Model(模型)表示应用程序核心(比如数据库记 ...
- BZOJ 3329 - Xorequ - 数位DP, 矩乘
Solution 发现 $x \ xor \ 2x = 3x$ 仅当 $x$ 的二进制中没有相邻的 $1$ 对于第一个问题就可以进行数位DP 了. 但是对于第二个问题, 我们只能通过递推 打表 来算 ...