PAT (Advanced Level) 1115. Counting Nodes in a BST (30)
简单题。统计一下即可。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; const int maxn=+;
struct Node
{
int left;
int right;
int val;
int dep;
} s[maxn]; int n;
int a[maxn];
int max_dep,n1,n2; void dfs(int x,int dep)
{
max_dep=max(max_dep,dep);
s[x].dep=dep;
if(s[x].left!=-) dfs(s[x].left,dep+);
if(s[x].right!=-) dfs(s[x].right,dep+);
} void DFS(int x)
{
if(s[x].dep==max_dep) n1++;
else if(s[x].dep==max_dep-) n2++;
if(s[x].left!=-) DFS(s[x].left);
if(s[x].right!=-) DFS(s[x].right);
} int main()
{
scanf("%d",&n); if(n==) printf("0 + 0 = 0\n");
else
{
for(int i=; i<=n; i++) scanf("%d",&a[i]); for(int i=; i<=n; i++) s[i].left=s[i].right=-;
int id=;
s[id++].val=a[]; for(int i=; i<=n; i++)
{
int now=;
while()
{
if(a[i]<=s[now].val)
{
if(s[now].left!=-) now=s[now].left;
else
{
s[now].left=id;
s[id++].val=a[i];
break;
}
} else
{
if(s[now].right!=-) now=s[now].right;
else
{
s[now].right=id;
s[id++].val=a[i];
break;
}
}
}
} max_dep=n1=n2=;
dfs(,);
DFS();
printf("%d + %d = %d\n",n1,n2,n1+n2);
}
return ;
}
PAT (Advanced Level) 1115. Counting Nodes in a BST (30)的更多相关文章
- 【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)
题意: 输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树.输出最底层和最底层上一层的结点个数之和,例如x+y=x+y. AAAAA ...
- [二叉查找树] 1115. Counting Nodes in a BST (30)
1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT Advanced 1115 Counting Nodes in a BST (30) [⼆叉树的遍历,BFS,DFS]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】
题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...
- PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)
题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...
- 1115. Counting Nodes in a BST (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT甲1115 Counting Nodes in a BST【dfs】
1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...
- PAT 1115 Counting Nodes in a BST[构建BST]
1115 Counting Nodes in a BST(30 分) A Binary Search Tree (BST) is recursively defined as a binary tre ...
- 1115 Counting Nodes in a BST (30 分)
1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...
随机推荐
- C# 二维数组和集合
本次课我主要学习了二维数组和集合的部分内容. 在二维数组的部分中,我主要学习了二维数组的定义方法:int [,] array=new int [4,2];做了一个小练习:用二维数组打印自己的姓氏. s ...
- sqlserver 按日、周、月统计方法
摘自于网络网络:http://blog.csdn.net/wanmdb/article/details/8080636 create table T(日期时间 datetime, 数量 int) in ...
- Android测试日志文件抓取与分析
1.log文件分类简介 实时打印的主要有:logcat main,logcat radio,logcat events,tcpdump,还有高通平台的还会有QXDM日志 状态信息的有:adb shel ...
- 怎么破解Wifi密码
破解无络网络Wifi密码,让手机上Wifi不再受限. 方法/步骤 1 上网搜索并下载“Wfi万能钥匙”APK程序,然后将其安装到手机内存中.可以借助手机类管理软件将APK应用安装到手机中. 步骤阅读 ...
- getTime 方法
转自http://www.yesky.com/imagesnew/software/javascript/html/jsmthgettime.htm getTime 方法返回 Date 对象中的时间值 ...
- hdu_1848_Fibonacci again and again(博弈sg函数)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 题意:给你3堆石子,每次只能取fibonacci数的石子,问先手是否能赢 题解:SG函数模版题 ...
- shell脚本 整数比较
1.整数比较 -eq 等于,如:if [ "$a" -eq "$b" ] -ne 不等于,如:if [ "$a" -ne " ...
- permutation求全排列
include <iostream> #include <string> using namespace std; void swap(char &c1, char & ...
- qmake pro工程设置
----我的生活,我的点点滴滴!! #以下是在terminal里面编译,想编译哪个就修改pro文件 #================================================= ...
- IntelliJ IDEA 2016.1.3激活【亲测可用】
测试日期:2016.6.24 License server: http://www.iteblog.com/idea/key.php // ========================= 更多技术 ...