文章

68

粉丝

0

获赞

0

访问

2.7k

头像
2014年计算机学科专业基础综合试题 - 第41题回答
数据结构
发布于2025年8月2日 16:38
阅读数 21

(1)先递归遍历左子树,判断该结点left和right指针是否为空,如果为空,则该结点为叶子结点,用其高度x对应的权值,计入总和,得到左子树的权值总和,然后递归遍历右子树,同理可得右子树的权值总和,再加起来得到WPL.

(2)typedef struct BiTNode{

int weight;

struct BiTNode * left;

struct BitNode * right;

}BitNode,*BiTree;

(3)

int calculateWPL(BiTree root) {
    return calculateWPLHelper(root, 0);
}
int calculateWPLHelper(BiTree node, int depth) {
    if (node == NULL) {
        return 0;  // 空结点贡献为0
    }
    // 如果是叶结点,返回 weight × depth
    if (node->left == NULL && node->right == NULL) {
        return node->weight * depth;
    }
    // 非叶结点,递归计算左右子树的WPL
    return calculateWPLHelper(node->left, depth + 1) + 
           calculateWPLHelper(node->right, depth + 1);
}

 


评分及理由

(1)得分及理由(满分3分)

得分:3分

理由:学生的设计思想与标准答案一致,描述了递归遍历二叉树并计算叶子结点的带权路径长度的过程,思路清晰且正确。

(2)得分及理由(满分4分)

得分:3分

理由:学生的数据类型定义基本正确,但存在拼写错误("BitNode"应为"BiTNode")。扣1分。

(3)得分及理由(满分6分)

得分:6分

理由:学生的算法实现与标准答案一致,递归逻辑正确,代码清晰且注释合理,无逻辑错误。

题目总分:3+3+6=12分

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发