PAT-A1115 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 [−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

题目大意

题目给出一个二叉查找树的插入序列,要求按照格式要求输出按序列插入后的二叉查找树低两层的节点总数。

解题思路

  1. 建立二叉查找树的插入过程;
  2. 读取序列并依次插入;
  3. 前序遍历并记录每层的节点个数;
  4. 输出低两层的个数并按格式要求输出;
  5. 返回零值。

代码

#include<cstdio>
#define maxn 1010
int level[maxn],maxheight=-1;

struct Node{
    int data;
    Node *lchild,*rchild;
};

Node* Create(int v){
    Node* root=new(Node);
    root->data=v;
    root->lchild=NULL;
    root->rchild=NULL;
    return root;
}

void Insert(Node* &root,int v){
    if(root==NULL){
        root=Create(v);
    }else{
        if(v>root->data){
            Insert(root->rchild,v);
        }else{
            Insert(root->lchild,v);
        }
    }
    return;
}

void PreOrder(Node* root,int depth){
    if(root==NULL){
        return;
    }
    level[depth]++;
    if(depth>maxheight){
        maxheight=depth;
    }
    PreOrder(root->lchild,depth+1);
    PreOrder(root->rchild,depth+1);
}

int main(){
    int N,a;
    Node* root=NULL;
    scanf("%d",&N);
    while(N--){
        scanf("%d",&a);
        Insert(root,a);
    }
    PreOrder(root,0);
    printf("%d + %d = %d\n",level[maxheight],level[maxheight-1],level[maxheight]+level[maxheight-1]);
    return 0;
}

运行结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
boosting-crowd-counting-via-multifaceted-attention是一种通过多方面注意力提升人群计数的方法。该方法利用了多个方面的特征来准确估计人群数量。 在传统的人群计数方法中,往往只关注人群的整体特征,而忽略了不同区域的细节。然而,不同区域之间的人群密度可能存在差异,因此细致地分析这些区域是非常重要的。 该方法首先利用卷积神经网络(CNN)提取图像的特征。然后,通过引入多个注意力机制,分别关注图像的局部细节、稀疏区域和密集区域。 首先,该方法引入了局部注意力机制,通过对图像的局部区域进行加权来捕捉人群的局部特征。这使得网络能够更好地适应不同区域的密度变化。 其次,该方法采用了稀疏区域注意力机制,它能够识别图像中的稀疏区域并将更多的注意力放在这些区域上。这是因为稀疏区域往往是需要重点关注的区域,因为它们可能包含有人群密度的极端变化。 最后,该方法还引入了密集区域注意力机制,通过提取图像中人群密集的区域,并将更多的注意力放在这些区域上来准确估计人群数量。 综上所述,boosting-crowd-counting-via-multifaceted-attention是一种通过引入多个注意力机制来提高人群计数的方法。它能够从不同方面细致地分析图像,并利用局部、稀疏和密集区域的特征来准确估计人群数量。这个方法通过考虑人群分布的细节,提供了更精确的人群计数结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值