代码拉取完成,页面将自动刷新
#include <stdio.h>
#include <stdlib.h>
typedef struct treeNode {
int weight;
struct treeNode* left, * right;
}TNode;
/**
* 创建二叉树
*/
void create(TNode* node, int c[], int pos, int length) {
if (pos < length) {//填入数据
node->weight = c[pos];
if (pos * 2 + 1 < length) node->left = (TNode*)malloc(sizeof(TNode));
if (pos * 2 + 2 < length) node->right = (TNode*)malloc(sizeof(TNode));
create(node->left, c, pos * 2 + 1, length);
create(node->right, c, pos * 2 + 2, length);
}
}
int getWPL(TNode* root, int depth) {
if (root != NULL) {
int leftWeight = 0;
int rightWeight = 0;
if (root->left != NULL)leftWeight = getWPL(root->left, depth + 1);
if (root->right != NULL)rightWeight = getWPL(root->right, depth + 1);
return depth * root->weight + leftWeight + rightWeight;
}
else return 0;
}
int main() {
TNode* t = (TNode*)malloc(sizeof(TNode));
int c[] = { 54,4654,7565,234,436546,876876,353,757,2,345,23,445,1,235,0,6346 };
create(t, c, 0, 16);
int i = getWPL(t, 0);
printf("%d", i);
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。