1115 Counting Nodes in a BST (30 分)

这道题我最开始题目理解错了,最开始我理解成了统计叶子节点和叶子节点的上一层的结点数量。一提交得了2分,这时我知道我题目肯定读错了。
后来又做了一次,然后提交得了8分,我实在想不出有什么地方没写对,但是就是通不过。
后来认真读了一遍题目,才发现这个二叉搜索树种有重复数值的结点,而且要按照题目,把小于等于根结点的数据放在左子树,我想当然的放在了右子树,所以导致了错误,后来把这个错误改正过来了,一提交,果然通过了。
这个故事告诉我们认真读题是多么的重要。
后来翻看别人博客的时候,看到有个很简洁的写法,所以就办了过来。在插入的时候,同时记录结点的层数。

简洁版

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
	int data;
	node *left,*right;
	node(int x): data(x),left(NULL),right(NULL){} //构造函数 
};
int maxL=0;
vector<int> level(1005,0); //初始化 
void insert(node* &root, int x,int depth){
	if(root == NULL){
		root = new node(x);
		level[depth]++; 
		maxL = max(depth, maxL); //之前这个地方写错了 ,这里应该取深度的最大值,而不是深度出现次数的最大值 
	}
	else if(x > root->data ){
		insert(root->right, x,depth+1);
	}else{
		insert(root->left, x,depth+1);
	}
} 


int main(){
	int n,data;
	node* root = NULL;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&data);
		insert(root, data,0);
	}
	printf("%d + %d = %d\n",level[maxL],level[maxL-1],level[maxL] + level[maxL-1]);
	return 0;
}

笨比版

#include<cstdio>
int n1,n2;
struct node{
	int data,depth;
	node *left,*right;
};
void insert(node* &root, int x){
	if(root == NULL){
		root = new node;
		root->data = x;
		root->left = root->right = NULL; 
	}
	else if(x > root->data ){
		insert(root->right, x);
	}else{
		insert(root->left, x);
	}
} 
int max=0;
void find(node* root){
	if(root->left != NULL){
		root->left->depth = root->depth +1;
		if(root->left->depth > max) max = root->left->depth;
		find(root->left);
	}
	if(root->right != NULL){
		root->right->depth = root->depth +1;
		if(root->right->depth > max) max = root->right->depth;
		find(root->right);
	}
}
void preorder(node* root){
	if(root == NULL) return;
	if(root->depth == max-1) n2++;
	else if(root->depth == max) n1++;
	preorder(root->left);
	preorder(root->right);
}
int main(){
	int n,data;
	node* root = NULL;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&data);
		insert(root, data);
	}
	root->depth = 0;
	find(root);
	preorder(root);
	printf("%d + %d = %d\n",n1,n2,n1+n2);
	return 0;
}
全部评论

相关推荐

07-18 14:03
门头沟学院 Java
点赞 评论 收藏
分享
06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
07-17 11:50
门头沟学院 Java
投递腾讯等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务