题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum:

n1 + n2 = sum

递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两层的节点个数,也可以直接一遍dfs,顺便存储各个层的节点数。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#define LEFT 1
#define RIGHT 2
using namespace std;
/*
建立BST,两次dfs
一开始直接用index表示二叉树
即节点i的左孩子为2*i,右孩子为2*i+1
但是这样就会导致超出存储大小。。。
*/
const int maxn=+; //一开始设置成了1000导致段错误
int a[maxn];
int maxlayer=;
int n1=,n2=;
int cnt;
struct Node{
int left=-,right=-;
int id=-;
int value;
}node[maxn];
/*
root is the current node
val is the value of root
fa is the father node of root
LR tells that root is left or right child of fa
*/
void insertBST(int root,int val,int fa,int LR){
if(root==-){
cnt++;
node[cnt].value=val;
if(LR==LEFT){
node[fa].left=cnt;
}
else{
node[fa].right=cnt;
}
return;
}
if(val<=node[root].value){
insertBST(node[root].left,val,root,LEFT);
}
else{
insertBST(node[root].right,val,root,RIGHT);
}
}
void dfs(int root,int layer){
if(root==-){
maxlayer=max(maxlayer,layer-);
return;
}
dfs(node[root].left,layer+);
dfs(node[root].right,layer+);
} void dfsAns(int root,int layer){
if(root==-){
return;
}
if(layer==maxlayer){
n1++;
}
if(layer==maxlayer-){
n2++;
}
dfsAns(node[root].left,layer+);
dfsAns(node[root].right,layer+);
}
int main()
{
int n;
scanf("%d",&n);
int a;
memset(node,-,sizeof(node));
scanf("%d",&a);
node[].value=a;
cnt=;
for(int i=;i<n;i++){
scanf("%d",&a);
insertBST(,a,-,-);
}
dfs(,);
dfsAns(,);
printf("%d + %d = %d",n1,n2,n1+n2);
return ;
}

PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)的更多相关文章

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

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

  2. PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs

    统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...

  3. PAT甲题题解-1049. Counting Ones-数学问题

    n位数,总共有0~10^n-1共计10^n个数那么所有数出现的总次数变为n*(10^n)个数1出现的次数便是十分之一,所以n位数中,1出现的次数为n*10^(n-1)知道这一个后,接下来就方便求了. ...

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

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

  5. PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历

    题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...

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

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

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

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

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

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

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

随机推荐

  1. IE 8 下sharepoint 2013 难看的字体的解决方案

    将 corev15.css 中的有关"Segoe UI","Segoe",Tahoma,移除即可. 一共二处 C:\Program Files\Common F ...

  2. div+css ie6图片之间有间隙的问题

    图片转换为快级元素就解决了 img{display:block;} 也可设置img属性img{vertical-align:top;}

  3. 基础知识整理汇总 - Java学习(一)

    java 语言规范及相关文档资源 Java源码:安装目录下 src.zip 文件 java文档:https://docs.oracle.com/en/java/ 语言规范:http://docs.or ...

  4. C结构体数组赋值

    #include <stdio.h> #include <stdlib.h> struct MyStruct { int a; char b; }; struct MyStru ...

  5. 开发指南专题十四:JEECG微云高速开发平台MiniDao 介绍

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/27068645   开发指南专题十四:J ...

  6. P4279 [SHOI2008]小约翰的游戏

    嘟嘟嘟 一道博弈论经典题,nim游戏. 只不过要考虑有奇数个石子为1的堆的时候,为Brother赢.剩下就是nim游戏了. 极简代码 #include<cstdio> using name ...

  7. JCR分区(WOS或Thomson Reuters或汤姆森 路透)和中科院分区(附网址及查询方法)

    https://blog.csdn.net/Sunflower02/article/details/81187569

  8. J-Link调试查看变量值总是显示<not in scope> 和<cannot evaluate>问题

    原文:https://blog.csdn.net/gmpy_tiger/article/details/50395719 MDK/Keil 中,J-Link调试查看变量值总是显示<not in ...

  9. docker push images login -u harbor 问题记录 https 证书

    1.[root@dev-100 Desktop]# docker login -u clouder -p engine harbor.xiaowei.com 2.docker tag busybox: ...

  10. day80

    昨日回顾 上节回顾: 中间件: -django请求生命周期: -中间件:对全局请求的修改,和全局响应的修改 -process_request:从上往下执行 -process_response:从下往上 ...