简单题。统计一下即可。

#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)的更多相关文章

  1. 【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)

    题意: 输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树.输出最底层和最底层上一层的结点个数之和,例如x+y=x+y. AAAAA ...

  2. [二叉查找树] 1115. Counting Nodes in a BST (30)

    1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  3. 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 ...

  4. PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】

    题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...

  5. PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)

    题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...

  6. 1115. Counting Nodes in a BST (30)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Prime Path(BFS)

    Prime Path Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  2. AIR文件操作:使用文件对象操作文件和目录 .

    来源:http://blog.csdn.net/zdingxin/article/details/6635376 在AIR中可以方便的对本地文件操作,不过上次做了个项目,发现还是有不少不方便的地方,比 ...

  3. python+appium使用记录

    最近在研究appium+appiumlibrary移动端的两个自动化测试库,特此将使用过程,粗略记录一下 1.环境搭建,略,自行百度. 2.查看apk包名及activity方法,自行百度. 3.基本步 ...

  4. Chapter 2 Open Book——16

    By Friday I was perfectly comfortable entering my Biology class, nolonger worried that Edward would ...

  5. DOM操作-遍历HTML文档内容

    基础:   JS nodeType返回类型:http://blog.csdn.net/qyf_5445/article/details/9232907 代码: <!DOCTYPE html> ...

  6. 1.Java为什么能跨平台运行?请简述原理

    因为它有虚拟机(JVM),JAVA程序不是直接在电脑上运行的,是在虚拟机上进行的,每个系统平台都是有自己的虚拟机(JVM),所以JAVA语言能跨平台. 1, java代码不是直接运行在CPU上,而是运 ...

  7. insertRule()与addRule()创建规则

    要向现有的样式表中添加新规则,需要使用insertRule()方法.这个方法接受两个参数:规则文本和表示在那里插入规则的索引.下面是一个例子: sheet.insertRule("body ...

  8. docker 基础命令二

    开启/停止/重启 查看当前正在运行容器docker ps 查看包括已经停止的所有容器docker ps -a 显示最新启动的一个容器docker ps -l 新建一个容器运行docker run 启动 ...

  9. 使用composer安装laravel

    跟具官方文档说:Laravel utilizes Composer to manage its dependencies. So, before using Laravel, you will nee ...

  10. js监听键盘方向键事件

    <SCRIPT language=javascript> document.onkeydown = chang_page; function chang_page() { || ) loc ...