码迷,mamicode.com
首页 > 其他好文 > 详细

2014,计算二叉树带权路径长度

时间:2019-10-20 16:20:56      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:一个   col   static   node   return   int   基于   调用   tno   

思想:基于先序遍历,用一个静态变量保存WPL把每个节点的深度作为参数传递

若为叶子结点,WPL=该节点深度*权值,若非叶子节点则递归调用

代码:

typedef struct BTNode
{
    int weight;
    struct BTNode *lchild,*rchild;

}BTNode,*BTree;
int WPL(BTree root)
{
    return WPL_CORE(root,0);
}
int WPL_CORE(BTree root,int deep)
{
    static wpl=0;
    if(root->lchild==NULL&&root->rchild==null)
        wpl+=deep*root->weight;
    if(root->lchild!=NULL)
        WPL_CORE(root->lchild,deep+1);
    if(root->rchild!=NULL)
        WPL_CORE(root->lchild,deep+1);
    return wpl;

}

 

2014,计算二叉树带权路径长度

标签:一个   col   static   node   return   int   基于   调用   tno   

原文地址:https://www.cnblogs.com/yangmenda/p/11707608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!