简单题。统计一下即可。

#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. CentOS安装配置JDK-7(.rpm)

    声明:本文转自:http://www.cnblogs.com/zhoulf/archive/2013/02/04/2891608.html 安装说明 系统环境:centos-6.3安装方式:rpm安装 ...

  2. hdu 5584 gcd/lcm/数学公式

    input T 1<=T<=1000 x y output 有多少个起点可以走n(n>=0)步走到(x,y),只能从(x,y)走到(x,y+lcm(x,y))/(x+lcm(x,y) ...

  3. 《JavaScript高级程序设计》读书笔记 ---数据类型

    ECMAScript 中有5 种简单数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number.String和Object——复杂数据类型,Object 本质上是由一组 ...

  4. 使用cocoapods的两个大坑的修改方法

    1.报错内容: [!] The dependency `ReactiveCocoa (= 2.1.8)` is not used in any concrete target. The depende ...

  5. CentOS 6.4 系统下的MySQL的主从库配置

    首先了解到一. 二一.(MySQL下创建用户并且赋予权限)root用户创建yong用户的SQL语句 CREATE USER 'yong'@'localhost' IDENTIFIED BY 'yong ...

  6. LYF模板连接.txt

    在解决方案里创建了一个新建网站,在其右键下添加一个模板后,准备使用模板的,在添加新建项里突然没有“使用模板页的web窗体”,顿时感觉悲剧... 解决方法:模板页代码---LYFMaterPage.Ma ...

  7. JQuery中常用方法备忘

    本文转载自博客园,原文地址 http://www.cnblogs.com/xzf158/archive/2008/10/14/logan.html 1.Window.onload 的JQuery方法 ...

  8. 在线协作沟通工具DesignBoard帮助设计团队更有效地进行沟通与版本管理

    设计稿呈现缺乏整体性.远程沟通效率低.多版本管理混乱,这可能是很多创业团队都面临的问题,并且这些问题都将直接影响到产品开发进度.国内创业团队彩程设计也曾面临同样的问题,后来他们在做某个新项目时尝试把设 ...

  9. Output Limit Exceed是什么情况引起的

    输入的格式错误 Output Limit Exceed是超过输出限制(OLE)错误,提示你的程序产生了过多的输出信息,一般是由于死循环造成的.你的程序发生上述错误的主要原因是循环语句while(sca ...

  10. 在Linux服务器上增加硬盘没那么简单【转】

    运维案例:HP服务器,LINUX系统在保障数据的前提下扩展/home分区 部门需求:研发部门提出需要在现有的服务器上扩容磁盘空间,以满足开发环境的磁盘需求.现有空间1.6T需要增加到2T. 需求调查分 ...