华为OD机试(C卷+D卷)2024真题目录(Java & c++ & python)
题目描述
给定长度为 n 的无序的数字数组,每个数字代表二叉树的叶子节点的权值,数字数组的值均大于等于1。
请完成一个函数,根据输入的数字数组,生成哈夫曼树,并将哈夫曼树按照中序遍历输出。
为了保证输出的二叉树中序遍历结果统一,增加以下限制:
二叉树节点中,左节点权值小于右节点权值,根节点权值为左右节点权值之和。当左右节点权值相同时,左子树高度小于等于右子树高度。
注意:
所有用例保证有效,并能生成哈夫曼树。
提醒:
哈夫曼树又称为最优二叉树,是一种带权路径长度最短的二叉树。
所谓树的带权路径长度,就是树中所有的叶节点的权值乘上其到根节点的路径长度(若根节点为 0 层,叶节点到根节点的路径长度为叶节点的层数)
输入描述
例如:由叶子节点:5 15 40 30 10,生成的最优二叉树如下图所示,该树的最短带权路径长度为:40 * 1 + 30 * 2 + 15 * 3 + 5 * 4 + 10 * 4 = 205。
输出描述
输出一个哈夫曼的中序遍历数组,数值间以空格分隔
示例1
输入
5
5 15 40 30 10
输出
40 100 30 60 15 30 5 15 10
说明:根据输入,生成哈夫曼树,按照中序遍历返回。所有节点中,左节点权值小于等于右节点权值之和。当左右节点权值相同时,左子树高度小于右子树。结果如上图所示。
解题思路
构建哈夫曼树。
哈夫曼树定义:
给定N个权值作为N个叶子结点,构造一棵二叉树,若该树的带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman Tree)。
构建方法:
- 取出两个最小的权值
- 放回两个最小权值的合并和
- 直至剩一个元素
可以发现涉及最小值的存取,那么用优先队列来提速。
题目的特定限制:
二叉树节点中,左节点权值小于右节点权值,根节点权值为左右节点权值之和。当左右节点权值相同时,左子树高度小于等于右子树高度。
这是为了保证输出的二叉树中序遍历结果统一
参考代码
#include <bits/stdc++.h>
using namespace std;
struct Node {
shared_ptr<Node> lchild;
shared_ptr<Node> rchild;
int weight;
int height;
Node(int weight, int height) : weight(weight), height(height) {}
Node(shared_ptr<Node> lchild, shared_ptr<Node> rchild, int weight, int height)
: lchild(lchild), rchild(rchild), weight(weight), height(height) {}
};
class Compare {
public:
bool operator()(shared_ptr<Node> a, shared_ptr<Node> b) {
if (a->weight != b->weight) {
return a->weight > b->weight;
} else {
return a->height > b->height;
}
}
};
void inOrder(shared_ptr<Node> root) {
if (root->lchild != nullptr) {
inOrder(root->lchild);
}
cout << root->weight << " ";
if (root->rchild != nullptr) {
inOrder(root->rchild);
}
}
int main() {
int n;
cin >> n;
priority_queue<shared_ptr<Node>, vector<shared_ptr<Node>>, Compare> pq;
for (int i = 0; i < n; i++) {
int w;
cin >> w;
pq.push(make_shared<Node>(w, 0));
}
while (pq.size() > 1) {
auto lc = pq.top();
pq.pop();
auto rc = pq.top();
pq.pop();
int fa_weight = lc->weight + rc->weight;
int fa_height = max(lc->height, rc->height) + 1;
pq.push(make_shared<Node>(lc, rc, fa_weight, fa_height));
}
auto root = pq.top();
inOrder(root);
cout << endl;
return 0;
}