1115 Counting Nodes in a BST

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 [−10001000] 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

比较简单的一道题目,
但是我做的时候的问题
1.建树的模板不是很熟练
2.要仔细审题,说实话,我没仔细审题,PAT有些题目看似轮廓相同,但是它有些地方就进修了修改,比如这道题目,我记得前面有道题木是 >=root的节点是建在右边(还有上一道题目,一般先输入postorder,在输入inorder,它竟然反过来了,生气生气),这道题目改成了<=root ,就这个地方,第一次做值得了8分 = =,然后再仔细瞅一瞅就30了

===================================================================
还有我做的稍微麻烦了一点,再bfs的时候就可以标记height了,要建一个 int height 数组,标记每一层的个数,这样更快,这是做的时候就没想那么多,可能刚才脑子转不动了,被上一个题目搞得心态炸了,还好这道题目比较可爱(**话有点多= = **)
还有更精益求精的小地方我觉得可以用构造函数来进一步优化(起码好看,看起来比较高大上),但是我就先不要求了也不是很难,目前要求是广度

#include<iostream>
#include<vector>
#include<queue> 
const int maxn=1005;
using namespace std;
struct node{
	int v,height;
	node *lchild,*rchild;
};
int n,maxh,n1,n2;
node* newnode(int v){
	node* root = new node;
	root->v = v;
	root->lchild = root->rchild = NULL;
	return root;
}
void insert(node* &root,int x){
	if(root == NULL){
		root = newnode(x);
		return;
	}
	if( x < root->v || x == root->v) insert(root->lchild,x);
	else insert(root->rchild,x);
}
void bfs(node* root){
	root->height = 1;
	queue<node*> q;
	q.push(root);
	while(!q.empty()){
		node* temp = q.front();
		if(maxh < temp->height) maxh = temp->height;
		q.pop();
		if(temp->lchild !=NULL){
			temp->lchild->height=temp->height+1;
			q.push(temp->lchild);
		}
		if(temp->rchild !=NULL){
			temp->rchild->height=temp->height+1;
			q.push(temp->rchild);
		}
	}
}
int main(){
	cin>>n;
	int x;
	node* root = new node;
	root=NULL;
	for(int i=0;i<n;i++){
		cin>>x;
		insert(root,x);
	}
	bfs(root);
	queue<node*> q1;
	q1.push(root);
	while(!q1.empty()){
		node* temp = q1.front();
		q1.pop();
		if(temp->height == maxh) n1++;
		if(temp->height == maxh - 1) n2++;
		if(temp->lchild !=NULL) q1.push(temp->lchild);
		if(temp->rchild !=NULL) q1.push(temp->rchild);
	}
	printf("%d + %d = %d",n1,n2,n1+n2);
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值