PAT1115:Counting Nodes in a BST
1115. Counting Nodes in a BST (30)
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 思路 简单题,打印出二叉搜索树最后两层的节点数之和,格式为:"n1 + n2 = sum(n1,n2)"。 1.根据输入建立二叉搜索树
2.前序遍历二叉树,用一个数组统计每一层的节点数,levels[i]代表第i层的节点数,用一个maxlevel不断更新最大层数。
3.输出levels[maxlevel] + levels[maxlevel - 1]。 代码
#include<iostream>
#include<vector>
using namespace std;
vector<int> levels;
int maxlevel = -1;
class node
{
public:
node(int _val):val(_val),left(nullptr),right(nullptr)
{
}
int val;
node* left;
node* right;
}; typedef node* treenode; void buildBST(treenode& root,int val)
{
if(val <= root->val && !root->left)
{
root->left = new node(val);
return;
}
if(val <= root->val && root->left)
{
buildBST(root->left,val);
return;
}
if(val > root->val && !root->right)
{
root->right = new node(val);
return;
}
if(val > root->val && root->right)
{
buildBST(root->right,val);
return;
}
} void countNodes(const treenode root,int level)
{
levels[level]++;
if(level > maxlevel)
maxlevel = level;
if(!root->left && !root->right)
return;
if(root->left)
countNodes(root->left,level + 1);
if(root->right)
countNodes(root->right,level + 1);
} int main()
{
int n;
while(cin >> n)
{
levels.resize(n + 1);
treenode root = new node(0);
cin >> root->val;
for(int i = 1;i < n;i++)
{
int tmp;
cin >> tmp;
buildBST(root,tmp);
}
countNodes(root,1);
cout << levels[maxlevel] << " + " << levels[maxlevel - 1] << " = " << levels[maxlevel] + levels[maxlevel - 1] << endl;
}
}
PAT1115:Counting Nodes in a BST的更多相关文章
- 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 ...
- 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 ...
- [二叉查找树] 1115. Counting Nodes in a BST (30)
1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 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 ...
- PAT_A1115#Counting Nodes in a BST
Source: PAT A1115 Counting Nodes in a BST (30 分) Description: A Binary Search Tree (BST) is recursiv ...
- A1115. Counting Nodes in a BST
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT A1115 Counting Nodes in a BST (30 分)——二叉搜索树,层序遍历或者dfs
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT 甲级 1115 Counting Nodes in a BST
https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...
- 1115. Counting Nodes in a BST (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- 《java入门第一季》之面向对象(包概述)
由于eclipse等ide的强大功能,使得建包,导包用一些快捷键就能完成.这里对包的概念做稍微的叙述,了解即可: 分包后使得项目更加清晰,提高代码维护性. 包: A:其实就是文件夹 ...
- R--线性回归诊断(一)
线性回归诊断--R [转载时请注明来源]:http://www.cnblogs.com/runner-ljt/ Ljt 勿忘初心 无畏未来 作为一个初学者,水平有限,欢迎交流指正. 在R中线性回 ...
- Leetcode_49_Anagrams
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42744709 Given an array of stri ...
- CUDA Cuts: Fast Graph Cuts on the GPU
原文出处: http://lincccc.blogspot.tw/2011/03/cuda-cuts-fast-graph-cuts-on-gpu_03.html 现在需要代理才能访问,所以就转载了. ...
- 【61】git项目实战的步骤总结
1.新建分支的步骤 git pull git checkout -b 分支号(task的后面的代号) 2.提交代码到远程仓库的步骤 git add . git commit -m "分支号+ ...
- 更改EBS服务器域名/IP
more: 341322.1 : How to change the hostname of an Applications Tier using AutoConfig 338003.1 : How ...
- ORACLE中用rownum分页并排序的SQL语句
ORACLE中用rownum分页并排序的SQL语句 以前分页习惯用这样的SQL语句: select * from (selectt.*,rownum row_num frommytable t ord ...
- 【43】Activity的几种LaunchMode及使用场景
standard 模式 这是默认模式,每次激活Activity时都会创建Activity实例,并放入任务栈中.使用场景:大多数Activity. singleTop 模式 如果在任务的栈顶正好存在该A ...
- SoC嵌入式软件架构设计
内存是SoC(System on Chip,片上系统)集成设计的重要模块,是SoC中成本比重较大的部分.内存管理的软硬件设计是SoC软件架构设计的重要一环,架构设计师必须要在成本和效率中取得平衡,做到 ...
- The 2nd tip of DB Query Analyzer
The 2nd tip of DB Query Analyzer Ma Genfeng (Guangdong Unitoll Servi ...