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
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
typedef long long ll;
using namespace std;
/*
感觉他这个倒数第一层和第二层,很有那种次短路的意味
同时维护两个值,这样的话,感觉还好吧 
*/ 

struct node{
	int val;
	node* ls,*rs;
	node(int v):val(v),ls(NULL),rs(NULL){};//又一种赋值方式 
};

void insert(int val,node* &now){//引用很神奇 
	if(now==NULL){//我所指向的指针为空,但是我是有地址的 
		now=new node(val);
		return;
	}
	if(val<=now->val)
		insert(val,now->ls);
	else
		insert(val,now->rs);	
}

int main(){
	int n,val;
	scanf("%d",&n);
	node *head=NULL; 
	for(int i=1;i<=n;i++){
		scanf("%d",&val);
		insert(val,head);
	}
	queue<node*> q;
	q.push(head);
	int n1=0,n2=0;
	while(!q.empty()){//层序遍历 
		n2=n1;//现在就是倒数第二层 
		n1=q.size();//第一层只有一个节点 
		for(int i=0;i<n1;i++){//遍历这层的所有节点 
			node* now=q.front();
			q.pop();
			if(now->ls) q.push(now->ls);
			if(now->rs) q.push(now->rs);
		}
	}
	printf("%d + %d = %d\n",n1,n2,n1+n2); 
	return 0;
}

我写的烂代码(天真的我还想用数组模拟二叉树,简直难死了)

#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;

const int maxn=1010;
typedef struct Tr{
	int val;
	Tr* ls;
	Tr* rs;
}Tr;

int lev[maxn];
int max_lev;

Tr* head;
void init(int val,Tr* &now){
	now=(Tr*)malloc(sizeof(Tr));
	now->val=val;
	now->ls=NULL;
	now->rs=NULL;
	lev[1]=1;
}

Tr* creat(int val,int h,int i){
	Tr* now=(Tr*)malloc(sizeof(Tr));
	now->val=val;
	now->ls=now->rs=NULL;
	max_lev=max(max_lev,h);
	lev[i]=h;
	return now;
}

void insert(int val,Tr* now,int h,int i){
	if(val<=now->val){
		if(now->ls==NULL)
			now->ls=creat(val,h+1,i);		
		else
			insert(val,now->ls,h+1,i);
	}
	else{
		if(now->rs==NULL)
			now->rs=creat(val,h+1,i);
		else
			insert(val,now->rs,h+1,i);
	}
}


int main(){
	int n,val;
	scanf("%d",&n);
	max_lev=1;
	for(int i=1;i<=n;i++){
		scanf("%d",&val);
		if(i==1)
			init(val,head);
		else
			insert(val,head,1,i);
	}
	int ans1=0,ans2=0;
	for(int i=1;i<=n;i++){
		if(lev[i]==max_lev)
			ans1++;
		else if(lev[i]+1==max_lev)
			ans2++;
	} 
	//printf("max-lev:%d\n",max_lev);
	printf("%d + %d = %d\n",ans1,ans2,ans1+ans2);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值