zoukankan      html  css  js  c++  java
  • A1115Counting Nodes in a BST (30分)

    一、技术总结

    1. 本题是一个排序二叉树问题,题意理解起来就是,给N个结点,将其生成排序二叉树,然后将最后两层的结点数统计加以输出。
    2. 关键就是一个insert函数的书写,以及使用一个num数组进行存储每一层的结点数,使用DFS深度搜索遍历,传参包括一个root排序二叉树,一个depth用于记录该结点的层数初始为0层。
    3. 使用maxdepth记录最大层数。
    4. 最后将num数组中的最后两层输出。

    二、参考代码

    #include<bits/stdc++.h>
    using namespace std;
    struct node{
    	int data;
    	node* rchild;
    	node* lchild;
    };
    
    void insert(node* &root, int x){
    	if(root == NULL){
    		root = new node;
    		root->data = x;
    		root->rchild = root->lchild = NULL;
    		return;
    	}else if(x <= root->data){
    		insert(root->lchild, x);
    	}else{
    		insert(root->rchild, x);
    	}
    } 
    vector<int> num(1000);
    int maxdepth = -1;
    void dfs(node* root, int depth){
    	if(root == NULL){
    		maxdepth = max(depth, maxdepth);
    		return;
    	} 
    	num[depth]++;
    	dfs(root->lchild, depth+1);
    	dfs(root->rchild, depth+1);
    }
    int main(){
    	int n, data;
    	scanf("%d", &n);
    	node* root = NULL;
    	for(int i = 0; i < n; i++){
    		scanf("%d", &data);
    		insert(root, data);
    	}
    	dfs(root, 0);
    	printf("%d + %d = %d", num[maxdepth-1], num[maxdepth-2], num[maxdepth-1] + num[maxdepth-2]);
    	return 0;
    }
    

    三、排序二叉树的基本操作

    //注释部分为排序二叉树的一些基本操作
    /*
    node* newNode(int v){
    	node* Node = new node;
    	Node->data = v;
    	Node->lchild = Node->rchild = NULL;
    	return Node;
    }
    void insert(node* &root, int x){
    	if(root == NULL){
    		root = newNode(x);
    		return;
    	}
    	if(x == root->data){
    		return;
    	}else if(x < root->data){
    		insert(root->lchild, x);
    	}else{
    		insert(root->rchild, x);
    	}
    }
    node* Create(int data[], int n){
    	node* root = NULL;
    	for(int i = 0; i < n; i++){
    		insert(root, data[i]);
    	} 
    	return root;
    }
    node* findMax(node* root){
    	while(root->rchild != NULL){
    		root = root->rchild;
    	}
    	return root;
    }
    node* findMin(node* root){
    	while(root->lchild != NULL){
    		root = root->lchild;
    	}
    	return root;
    }
    void deleteNode(node* &root, int x){
    	if(root == NULL) return;
    	if(root->data == x){
    		if(root->lchild == NULL && root->rchild == NULL){
    			root = NULL;
    		}else if(root->lchild != NULL){
    			node* pre = findMax(root->lchild);
    			root->data = pre->data;
    			deleteNode(root->lchild, pre->data); 
    		}else{
    			node* next = findMin(root->rchild);
    			root->data = next->data;
    			deleteNode(root->rchild, next->data);
    		}
    	}else if(root->data > x){
    		deleteNode(root->lchild, x);
    	}else{
    		deleteNode(root->rchild, x);
    	}
    }
    */
    
    作者:睿晞
    身处这个阶段的时候,一定要好好珍惜,这是我们唯一能做的,求学,钻研,为人,处事,交友……无一不是如此。
    劝君莫惜金缕衣,劝君惜取少年时。花开堪折直须折,莫待无花空折枝。
    曾有一个业界大牛说过这样一段话,送给大家:   “华人在计算机视觉领域的研究水平越来越高,这是非常振奋人心的事。我们中国错过了工业革命,错过了电气革命,信息革命也只是跟随状态。但人工智能的革命,我们跟世界上的领先国家是并肩往前跑的。能身处这个时代浪潮之中,做一番伟大的事业,经常激动的夜不能寐。”
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    【3.1】学习C++之再逢const
    【8】学习C++之this指针
    【7】学习C++之类的构造函数
    【6】学习C++之类的实例化及访问
    【5】学习C++之类的概念
    【4】学习C++之内存管理
    【3】学习C++之const关键字的使用
    【2】学习C++之引用
    【C#第一天】数据相关
    【1】学习C++时,一些零散知识点01
  • 原文地址:https://www.cnblogs.com/tsruixi/p/12989760.html
Copyright © 2011-2022 走看看