求二叉树的带权路径长度 WPL。
结点结构:
left | weight | right |
---|
typedef struct node{
node* left;
int weight;
node* right;
} Node;
int WPL(Node* root,int depth){
if(root!=NULL){
return root->weight*depth+WPL(root->left,depth+1)+WPL(root->right,depth+1);//递归计算,每个子树的WPL=自身weight*depth+左右两棵子树的WPL
}
return 0;
}