简单题。统计一下即可。

#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. XueTr 0.45 (手工杀毒辅助工具) 绿色版

    软件名称: XueTr 0.45 (手工杀毒辅助工具)软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 3.3MB图片预览: ...

  2. call ,apply 和 bind的用法与区别

    作用都是一样的,官方解释:"调用一个对象的一个方法,以另一个对象替换当前对象", 简单来说就是改变当前使用该方法的对象中的this指向: var xw = { name : &qu ...

  3. C# 获取字符的Unicode编码

    using UnityEngine;using System.Collections;using System.Collections.Generic; List<); string chars ...

  4. 关于C++中字符的转换

    VS的工程属性中我们常会设置Character Set:

  5. linux中将程序加入到开机自动启动

    如果将在linux中将命令或者程序设置为开机自动启动,只需要进入到将对应命令加入到/etc/rc.d/rc.local文件里即可,如下 打开文件,vi /etc/rc.d/rc.local #!/bi ...

  6. 做环信聊天布局时遇到的问题(UITextView自适应高度和光标位置不正常)

    聊天时UITextView发送消息超过最大高度后点击发送按钮同时将文本清空,光标的位置偏离了正常的轨迹.如图 后研究发现UITextView向上滚动的时候,contentOffset.y都会发生改变, ...

  7. j2ee的十三种技术

    J2EE的十三种技术(规范)  种不同的驱动,具体来说,包括有: 类型1:JDBC-ODBC桥 在JDBC刚产生时,JDBC-ODBC桥是非常有用的.通过它,开发者可以使用JDBC来访问一个ODBC数 ...

  8. php5.6之后的版本使用curl以@+文件名的方式上传文件无效的解决版本

    使用curl上传文件使用file=@文件路径的方式,在php5.6以后的版本中无法使用了 官方文档给出明确解释 如果需要支持的话,可以将CURLOPT_SAFE_UPLOAD设置为false 或者使用 ...

  9. Hibernate4 占位符(?)

    Hibernate3使用?占位符: Session session = sessionFactory.getCurrentSession();  session.beginTransaction(); ...

  10. 笨方法学python--简介

    该章提到编程新手所需的三种最重要的技术:读和写,注重细节,发现不同. 读 和 写 即熟悉代码中的各种字符. 注 重 细 节 将例子一字不差地打出来,通过实践训练自己 发 现 不 同 这个是通过长年累月 ...