max sum of subtree


//@author zengkui111@gmail.com


struct node 

{

    int node_id;

    int number;

    bool has_negative_node;

    int sum_of_subtree;

    struct node *left;

    struct node *right;

};


// the return value is the node id which the sum of the subtree is max , -1 is not find

int find_max_sum_of_subtree( struct node *root, const bool &negative_is_ok )

{

    if ( NULL == root -> left and NULL == root->right )    

    {

        //leaf node

        root->sum_of_subtree = root->number;    

        if ( root->number < 0 )

        {

            root->has_negative_node = true;

        }

        if ( !negative_is_ok )

        {

            if ( root->number > 0 )

            {

                return node_id;

            }

            else 

            {

                //not find any node 

                return -1;

            }

        }

        return root->node_id;

    }

    //The max sum of subtree is from left child or right child or this whole subtree 

    root->sum_of_subtree  = root->number;

    int left_id = -1;

    int right-id = -1;

    root->has_negative_root = false;


    if ( root->number < 0 )

    {

        root->has_negative_root = true;

    }

    if ( root -> left )

    {

        left_id = find_max_sum_of_subtree ( root->left, negative_is_ok );    

        root->sum_of_subtree  += root->left->sum_of_subtree; 

        if ( root->left->has_negative_root )

        {

            root->has_negative_root = true;

        }

    }

    if( root -> right )

    {    

        right_id = find_max_sum_of_subtree ( root->right, negative_is_ok );    

        root->sum_of_subtree  += root->right->sum_of_subtree; 

        if ( root->right->has_negative_root )

        {

            root->has_negative_root = true;

        }

    }


    int max_sum = (1<<31);

    int node_idx = -1;

    if ( ! negative_is_ok )

    {

        if ( root->sum_of_subtree > max_sum and !root->has_negative_root )

        {

            max_sum = root->sum_of_subtree;

            node_idx = root->node_id;

        }

        if ( root->left and root->left->sum_of_subtree > max_sum  and !root->left->has_negative_root )

        {

            max_sum = root->left->sum_of_subtree;

            node_idx = root->node_id;

        }

        if ( root->right and root->right->sum_of_subtree > max_sum and !root->right->has_negative_root )

        {

            max_sum = root->right->sum_of_subtree;

            node_idx = root->node_id;

        }

        return node_idx;

    }

    if ( root->sum_of_subtree > max_sum)

    {

        max_sum = root->sum_of_subtree;

        node_idx = root->node_id;

    }

    if ( root->left and root->left->sum_of_subtree > max_sum)

    {

        max_sum = root->left->sum_of_subtree;

        node_idx = root->node_id;

    }

    if ( root->right and root->right->sum_of_subtree > max_sum) 

    {

        max_sum = root->right->sum_of_subtree;

        node_idx = root->node_id;

    }

    return node_idx;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值