1115 Counting Nodes in a BST (30 分) 全网最细 题目详解 翻译 完整代码 PAT甲级真题解析

预备知识:

  1. 二叉树排序树的基本知识

题目翻译:

二叉搜索树(BST)本质是一棵二叉树,它具有以下特性:

  • 一个节点的左子树的结点值均只包含键值小于或等于该节点键值的节点。一
  • 一个节点的右子树只包含键大于该节点键的节点。
  • 左子树和右子树也必须是二叉搜索树。

在一棵空的二叉搜索树中插入一系列结点。然后,你需要计算树中最后两层的节点个数。

输入格式:

每个输入文件包含一个测试用例。对于每个用例,第一行给出一个正整数N(≤1000)表示这是输入序列的数字个数。下一行给出的是N个正整数,范围在[−1000,1000],将他们按照要求插入初始为空的二叉搜索树中。

输出格式:

对于每种情况,在一行中打印该树的最后两层的节点数,格式如下:
n1 + n2 = n
其中n1为最后一层的节点数,n2为倒数第二层的节点数,n为n1和n2的和。

输入样例:

9
25 30 42 16 20 20 35 -5 28

输出样例:

2 + 4 = 6

题目解析:

二叉搜索树的建立与遍历,简单题

  1. 建立二叉搜索树
  2. 二叉搜索树的遍历
  3. 输出

逻辑梳理:

  1. 搜索二叉树的结点定义与一般二叉树类似:

    struct node {
        int v;
        struct node *left, *right;
    };
    
  2. 递归建立二叉搜索树:
    a. 若当前所处的结点为空,说明找到了位置,用传入的目标值初始化当前结点;
    b. 若目标值小于等于当前结点的值,按照BST的规则需要向左子树递归;
    c. 若目标值大于当前结点的值,按照BST的规则需要向右子树递归;
    d. 递归结束后return当前结点信息(最后将返回根结点)。

    // main函数中的代码
    node *root = NULL;
    for(int i = 0; i < n; i++) { // n为结点总数 t为当前结点的值
        scanf("%d", &t);
        root = build(root, t);
    }
    
    // 建立搜索二叉树函数
    node* build(node *root, int v) {
        if(root == NULL) {
            root = new node();
            root->v = v;
            root->left = root->right = NULL;
        } else if(v <= root->v)
            root->left = build(root->left, v); // 递归建立并链接
        else
            root->right = build(root->right, v); // 递归建立并链接
        return root;
    }
    
    
    
  3. 二叉搜索树的遍历:
    a. 遍历每一个结点,并将他们的层数记录下来,累加到一个初始值全为0的数组中(数组下标+1 = 层数);
    b. 遍历同时需要不断更新最大层数,方便输出;
    c. 采用任意遍历方法均可。

    vector<int> num(1000); // num储存每一层的结点数
    int maxdepth = -1; // 最大层数
    void dfs(node *root, int depth) {
        if(root == NULL) {
            maxdepth = max(depth, maxdepth); // 更新最大层数
            return;
        }
        num[depth]++;
        dfs(root->left, depth + 1); // 左递归
        dfs(root->right, depth + 1); // 右递归
    }
    
  4. 最后输出,没什么好说的,直接看代码。

参考代码:

#include <iostream>
#include <vector>
using namespace std;
struct node {
    int v;
    struct node *left, *right;
};

// 建立搜索二叉树
node* build(node *root, int v) {
    if(root == NULL) { // 若当前所处的结点为空,说明找到了位置,用传入的目标值初始化当前结点
        root = new node();
        root->v = v;
        root->left = root->right = NULL;
    } else if(v <= root->v) // 若目标值小于等于当前结点的值,按照BST的规则需要向左子树递归
        root->left = build(root->left, v);
    else // 若目标值大于当前结点的值,按照BST的规则需要向右子树递归
        root->right = build(root->right, v);
    return root;
}

vector<int> num(1000); // num储存每一层的结点数
int maxdepth = -1; // 最大层数
void dfs(node *root, int depth) {
    if(root == NULL) {
        maxdepth = max(depth, maxdepth); // 更新最大层数
        return;
    }
    num[depth]++;
    dfs(root->left, depth + 1); // 左递归
    dfs(root->right, depth + 1); // 右递归
}
int main() {
    int n, t;
    scanf("%d", &n);
    node *root = NULL;
    for(int i = 0; i < n; i++) {
        scanf("%d", &t);
        root = build(root, t);
    }
    dfs(root, 0);
    printf("%d + %d = %d", num[maxdepth-1], num[maxdepth-2], num[maxdepth-1] + num[maxdepth-2]);
    return 0;
}

全部通过
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值