https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904
很经典的构建搜索二叉树。

#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
int l[N],r[N],w[N],idx,n,x;
int cnt[N],maxv;
void insert(int &u,int c)//注意一定要引用。
{if(!u)//无结点{u=++idx;//构建结点w[u]=c;}else if(w[u]>=c) insert(l[u],c);//左边插入else insert(r[u],c);//右边插入
}
void dfs(int u,int deep)
{cnt[deep]++;maxv=max(maxv,deep);if(l[u]) dfs(l[u],deep+1);if(r[u]) dfs(r[u],deep+1);
}
int main(void)
{int root=0;cin>>n;for(int i=0;i<n;i++){cin>>x;insert(root,x);}dfs(root,0);printf("%d + %d = %d\n",cnt[maxv],cnt[maxv-1],cnt[maxv]+cnt[maxv-1]);return 0;
}

1115 Counting Nodes in a BST (30 分)【难度: 一般 / 知识点: 构建二叉搜索树】相关推荐

  1. PAT甲级1115 Counting Nodes in a BST (30分):[C++题解] 递归建二叉搜索树、dfs求一层结点数量

    文章目录 题目分析 题目链接 题目分析 分析 首先本题给定的二叉搜索树的定义和其他地方的不同.本题小于等于的是左子树,右子树是大于根结点的. 然后说一下做题的思路. 给定一串数据,让构造二叉搜索树. ...

  2. PAT甲级1064 Complete Binary Search Tree (30分):[C++题解]完全二叉搜索树BST

    文章目录 题目分析 题目链接 题目分析 思路: 第一步,构造含有n个结点的完全二叉树:第二步,将n个数值填入,使其满足二叉搜索树的性质. 对于第一步: 完全二叉树用一维数组可以存下,不过从根结点的下标 ...

  3. PAT甲级1099 Build A Binary Search Tree (30分):[C++题解]建立二叉搜索树、dfs和bfs

    文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的结构,和待填的数值,请将数据填到二叉树中的结点中,使之满足二叉搜索树的性质. 然后按照层序遍历输出数值. 分析: 本题分两步. 第一步 ...

  4. 1115. Counting Nodes in a BST (30) 数据结构

    给出一颗BST树 求这个数最后两层的节点数量 用c1+c2 = n 的形式给出 建树深度搜索即可 #include<iostream> #include<cstdio> #in ...

  5. 1099 Build A Binary Search Tree (30 分)【难度: 一般 / 知识点: 建立二叉搜索树】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 首先: 要知道二叉搜索树的中序遍历是有序的 ...

  6. 1064 Complete Binary Search Tree (30 分)【难度: 一般 / 知识点: 完全二叉搜索树】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 二叉搜索数的中序遍历是有序的.故先将权值排序 ...

  7. 1043 Is It a Binary Search Tree (25 分)【难度: 中 / 知识点: 构造二叉搜索树(BST) 】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 首先二叉搜索树是左子树的元素都小于根,左子树 ...

  8. 1115 Counting Nodes in a 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

    我的DFS void DFS(Node* root){if(root==NULL)return;if(root->lchild){root->lchild->layer = root ...

最新文章

  1. 【AI】吴恩达斯坦福机器学习中文笔记汇总
  2. 进程 线程 协程_进程,线程,协程那些事
  3. Lync Server 2010标准版系列PART1:基础构建
  4. centos重启网络失败Job for network.service failed,ping不通网络
  5. 支撑微博千亿调用的轻量级RPC框架:Motan
  6. 2021-07-24
  7. Excel-开发者工具(WPS)
  8. c语言 大数相加,c/c++开发分享C语言计算大数相加的方法
  9. linux无后缀名程序运行,linux – 如何在Ubuntu上运行无扩展(也许是ELF)文件?
  10. catia如何画花键_CATIA到底有多用呢~
  11. php7版本搭建sqli labs,CentOS 7 LAMP搭建并且部署sqli-labs
  12. Colaboratory读取谷歌云盘(Google drive)中的数据(2020年3月28日更新)
  13. 在OS X中使用Homebrew
  14. python 物体定位_python中使用DNF Extractor和PIL定位dnf中物品位置
  15. 显卡mx150和230哪个好_MX130与MX150差距对比分析
  16. Python3安装turtle报错 File “/tmp/pip-install-hpqxw6_s/turtle/setup.py”, line 40
  17. Java工具类Result<T>
  18. 毕设 JAVA.JSP二手车交易系统论文
  19. 2016暑假集训小结
  20. 干货满满!25种绝佳VSCode扩展,体验便捷高效的生活

热门文章

  1. pyhanlp 文本分类与情感分析
  2. 决策树-基于不同算法的决策树模型对比
  3. openstack搭建之-nova配置(10)
  4. MongoDB基本命令总结
  5. 201571030335/201571030320《小学四则运算练习软件软件需求说明》结对项目报告
  6. Android 笔记之 R 文件
  7. Sersync实现触发式文件同步
  8. DevExpress ASP.NET 使用经验谈(9)-Dev控件客户端事件 ClientSideEvents
  9. 批量正则替换某文件夹中代码(div id=XXXX替换为div id=XXXX)
  10. 为什么linux的TTY登录程序getty/agetty一般都会设置一个-L的option?