zoukankan      html  css  js  c++  java
  • A1115 Counting Nodes in a BST [bst/dfs]

    在这里插入图片描述

    #include<iostream>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<string>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    const int maxn = 1001;
    struct node
    {
    	int data;
    	node* left,* right;
    }Node[maxn];
    void insert(node* &root, int data)
    {
    	if (root == NULL)
    	{
    		root = new node;
    		root->data = data;
    		root->left = root->right = NULL;
    		return;
    	}
    	if (data <= root->data) insert(root->left, data);
    	else insert(root->right, data);
    }
    int num[maxn] = { 0 }, maxdepth = 0;
    void dfs(node* root,int depth)
    {
    	if (root == NULL) return;
    	num[depth]++;
    	maxdepth = max(depth, maxdepth);
    	dfs(root->left, depth + 1);
    	dfs(root->right, depth + 1);
    }
    int main()
    {
    	int n,data; cin >> n;
    	node* root = NULL;
    	for (int i = 0; i < n; i++)
    	{
    		cin >> data;
    		insert(root, data);
    	}
    	dfs(root, 1);
    	int ans = num[maxdepth] + num[maxdepth - 1];
    	cout << num[maxdepth] <<" + "<< num[maxdepth - 1]<<" = "<<ans << endl;
    }
    
  • 相关阅读:
    (22)C#windows打包部署
    (2)OLEDB数据库操作
    (5)C#工具箱-数据
    (21)C#VS快捷键
    (1)OracleClient数据库操作(淘汰)
    (4)C#工具箱-菜单和工具栏
    (3)C#工具箱-容器
    (2)C#工具箱-公共控件2
    (9)oracle 表的基本查询
    企鹅
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13812005.html
Copyright © 2011-2022 走看看