题目

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

The left subtree of a node contains only nodes with keys less than or equal to the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
Insert a sequence of numbers into an initially empty binary search tree. Then you are supposed to count the total number of nodes in the lowest 2 levels of the resulting tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=1000) which is the size of the input sequence. Then given in the next line are the N integers in [-1000 1000] which are supposed to be inserted into an initially empty binary search tree.

Output Specification:

For each case, print in one line the numbers of nodes in the lowest 2 levels of the resulting tree in the format:

n1 + n2 = n

where n1 is the number of nodes in the lowest level, n2 is that of the level above, and n is the sum.

Sample Input:
9
25 30 42 16 20 20 35 -5 28
Sample Output:
2 + 4 = 6


题解

#include<iostream>
#include<vector>
using namespace std;
struct node{int v;struct node *left,*right;
};
node* build(node *root,int v){if(root==NULL){root=new node();root->v=v;root->left=root->right=NULL;}else if(v<=root->v){root->left=build(root->left,v);}else{root->right=build(root->right,v);}return root;
}
vector<int> num(1000);
int maxdepth=-1;
void dfs(node *root,int depth){if(root==NULL){maxdepth=max(maxdepth,depth);return;}num[depth]++;dfs(root->left,depth+1);dfs(root->right,depth+1);
}
int main(){int n,m;scanf("%d",&n)  ;node *root=NULL;for(int i=0;i<n;i++){scanf("%d",&m);root=build(root,m);}dfs(root,0);printf("%d + %d = %d",num[maxdepth-1],num[maxdepth-2],num[maxdepth-1] + num[maxdepth-2]);//等号前也有空格,格式要重视return 0;
}

回顾指针这个概念,它是存储着某变量的地址。

PAT甲级1115 DFS和BST相关推荐

  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甲级1147 Heaps (30 分):[C++题解]堆、树的遍历、dfs、完全二叉树建树

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:给定完全二叉树,判断是否是堆,需要区分大根堆,小根堆.后面是输出后序遍历. AC代码 #include<bits/stdc++. ...

  4. PAT甲级1130 Infix Expression:[C++题解]中缀表达式、二叉树中序遍历、dfs

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:本题是借助中缀表达式这个背景,考察二叉树的中序遍历.本题需要注意的地方是加括号. 左子树和右子树无脑加括号,只要不是叶结点. 所以写d ...

  5. PAT甲级1155 Heap Paths (30 分):[C++题解]堆、堆的遍历、树的遍历、dfs输出路径、完全二叉树建树

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 堆首先是完全二叉树,所以先建完全二叉树,由于给定的是层序遍历的数据,所以直接用数组即可,注意数组下标从1开始,这样便满足结点u和左儿 ...

  6. PAT甲级1053 Path of Equal Weight (30分) :[C++题解]dfs求树的路径长度、邻接表

    文章目录 题目分析 题目链接 题目分析 输入样例: 20 9 24 10 2 4 3 5 10 2 18 9 7 2 2 1 3 12 1 8 6 2 2 00 4 01 02 03 04 02 1 ...

  7. PAT甲级1119 Pre- and Post-order Traversals (30分):[C++题解]暴搜dfs、前序遍历和后序遍历求中序遍历

    文章目录 题目分析 题目链接 题目分析 分析 给了前序遍历和后序遍历,能够确定根结点,但是左子树和右子树的长度是不确定的.这里采用的解决方案是枚举左子树的结点个数,其实右子树的结点个数也确定了.对于每 ...

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

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

  9. PAT甲级1043 Is It a Binary Search Tree :[C++题解]判断二叉搜索树BST、给定前序序列和中序序列

    文章目录 题目分析 题目链接 题目分析 二叉搜索树(BST):左子树小于根结点,右子树大于等于根结点. 二叉搜索树的中序遍历一定是有序序列.所谓中序遍历:先访问左子树,再访问根结点,最后访问右子树. ...

最新文章

  1. 基于qml创建最简单的图像处理程序(1)-基于qml创建界面
  2. SpringMVC杂记
  3. 指针变量和引用变量的区别_指针Ⅰ--变量的定义
  4. hadoop yarn集群HA安装及配置
  5. C# HttpWebRequest提交数据方式
  6. JDBC对MySQL数据库存储过程的调用
  7. CentOS6.2下使用Nokia E72i成功发送短信
  8. 无线路由器在手机上如何连接服务器,192.168.10.1路由器手机怎么设置? | 192路由网...
  9. 回顾Swift 3,展望Swift 4
  10. bcb series清除前面的点_新iPhone前面板曝光:丑刘海还在,但边框窄了
  11. 设置devenv.exe启动版本(转)
  12. php实现微信网页授权回调代理
  13. ASP.NET 2.0之HtmlInputFile控件使用小结
  14. java MD5 32位加密
  15. 2014小米校园招聘笔试(10.13北京)
  16. 使用ffmpeg解析mp4文件得到音频和视频数据
  17. 动态电路中的动态元件——电容和电感
  18. Unity3D开发之----将预设体Prefab压缩成AssetBundle文件
  19. python工作技巧_能让你工作事半功倍的python小技巧大合集
  20. 逝者:Django贡献最多的核心开发者Malcolm Tredinnick

热门文章

  1. linux 常用压缩格式,Linux常见压缩格式之压缩与解压
  2. java lock代码写法_[代码全屏查看]-java lock的使用
  3. php数据库find(),db_find()
  4. 洛谷P1079 Vigenère 密码
  5. weblogic对JSP预编译、weblogic读取JSP编译后的class文件、ant中weblogic.jspc预编译JSP
  6. spring集成quartz框架
  7. Error: Trying to remove yum, which is protected
  8. IE下easyui 缓存问题
  9. 使用dmidecode获取常用的硬件信息
  10. 两个offer如何做选择?年薪20万vs年薪15万