题目:二叉排序树,统计最后两层节点个数

思路:数组格式存储,insert建树,dfs遍历

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int maxn = 1e5 + 10;
int n, cnt[maxn], a[maxn];
int ch[maxn][2], root; void insert(int &x,int y)
{
if(!x){x=y;return;}
if(a[y]<=a[x])insert(ch[x][0],y);
else insert(ch[x][1],y);
} void dfs(int x,int dep)
{
if(!x)return;
cnt[dep]++;
dfs(ch[x][0],dep+1);
dfs(ch[x][1],dep+1);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
insert(root,i);
}
dfs(root,1);
for(int i=n;i;i--)
{
if(cnt[i])
{
printf("%d + %d = %d\n",cnt[i],cnt[i-1],cnt[i]+cnt[i-1]);
break;
}
}
return 0;
}

PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】的更多相关文章

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

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

  4. PAT 甲级 1115 Counting Nodes in a BST

    https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...

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

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

  6. PAT (Advanced Level) 1115. Counting Nodes in a BST (30)

    简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

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

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

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

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

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

随机推荐

  1. matlab进阶:常用功能的实现,常用函数的说明

    常用功能的实现 获取当前脚本所在目录 current_script_dir = fileparts(mfilename('fullpath')); % 结尾不带'/' 常用函数的说明 bsxfun m ...

  2. Nginx简易配置文件(二)(反向代理)

    user nobody nobody; worker_processes 4; error_log logs/error.log; pid logs/nginx.pid; events { use e ...

  3. python更新后yum问题

    How to switch between Python versions on Fedora Linux Currently, the default python version on Fedor ...

  4. hdu5468 Puzzled Elena

    hdu5468 Puzzled Elena 题意 求一棵子树内与它互质的点个数 解法 容斥 我们先求出与它不互质的数的个数,再用总数减去就好. #include <cstdio> #inc ...

  5. <<< 判断提交方式是get还是post

    if("GET".equals(request.getMethod())){ System.out.println("提交方式是GET"); }else if( ...

  6. 【原】fiddler修改线上的内容

    摘要:当我们线上的代码出bug了,咋办呢?有时候本地的代码跟线上的代码还是运行环境还是有区别的.比如有些封装的方法需要运动到手机上可以调试,而浏览器是无法调试的.如果不想每次修改完再放上到测试环境看效 ...

  7. 战神Z7 D2安装黑苹果OS X El Capitan 10.11.2

    安装之初状态:两块硬盘,都是MBR格式分区,一块是机械硬盘,安装了WIN7 32位和Linux,一块是SSD,安装的是WIN7 64位与WIN10 64位以前玩过Mavericks,安装的就是在硬盘的 ...

  8. Android基础篇(一)

    Android体系结构介绍 Android是一个移动开发平台,层次结构:操作系统(OS).中间件(Middle Ware).应用程序(Application) 具体: 操作系统(OS)-->各种 ...

  9. PHP数组函数: array_walk()与 array_map() 的区别

    详细的介绍如下: PHP数组函数: array_walk() PHP数组函数: array_map() 实际应用中的一点区别与总结: array_walk() 主要用于对某个数组的迭代,相当于 for ...

  10. GitHub使用教程

    一直以来都想使用Git来管理自己平时积累的小代码,就是除了工作之外的代码了.有时候自己搞个小代码,在公司写了,就要通过U盘或者网盘等等一系列工具进行Copy,然后回家才能继续在原来的基础上作业.Cop ...