數(shù)據(jù)結(jié)構(gòu)拓展習(xí)題:二叉樹的帶權(quán)路徑長(zhǎng)度WPL
2022-05-28 00:01 作者:回到唐朝當(dāng)少爺 | 我要投稿
題目:二叉樹的帶權(quán)路徑長(zhǎng)度(WPL)是二叉樹中所有葉結(jié)點(diǎn)的帶權(quán)路徑長(zhǎng)度之和。給定一棵二叉樹T,采用二叉鏈表存儲(chǔ), 結(jié)點(diǎn)結(jié)構(gòu)為:(lchild,weight,rchild) ,其中葉結(jié)點(diǎn)的weight域保存該結(jié)點(diǎn)的非負(fù)權(quán)值。設(shè)root為指向T的根結(jié)點(diǎn)的指針,請(qǐng)?jiān)O(shè)計(jì)求T的WPL的算法。



int WPL(BiTree T, int depth)
{
?????? if (T == NULL)
????????????? return 0;
?????? if (T->lchild == NULL && T->rchild == NULL)
????????????? return T->data * depth;
?????? return WPL(T->lchild, depth + 1) + WPL(T->rchild, depth + 1);
}
標(biāo)簽: