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

max sbustree

时间:2018-01-16 18:24:59      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:other   ==   body   tps   .com   find   tca   binary   code   

这是亚麻的OA 题

这里的代码没有经过测试

Given a binary tree, find the subtree with maximum sum. Return the root of the subtree.

 

another similar problem in leetcode :

https://leetcode.com/problems/most-frequent-subtree-sum/description/

which has passed all the testcases.

struct TreeNode {
    TreeNode* right = null;
    TreeNode* left =null ;
    int val  =0;
}

int  _MaxSubtree ( TreeNode* root , TreeNode* & res , int maxvalue){

    if ( root == null  ) return 0;

    int vall = _MaxSubtree( root.left , res, maxvalue);
     int valr  =_MaxSubtree (root.right, res. maxvalue);
    
    int val = root.val + vall + valr;
    if ( val > res ){
        res = root;
        maxvalue = val; 
    }

    return val;

    }
}

TreeNode * MaxSubtree ( TreeNode* root ){
    if (root == null ) return null;

    TreeNode* res == null;
    int maxval = INT_MIN;

    _ MaxSubtree( root, res, maxval);

    return res;
}

 

max sbustree

标签:other   ==   body   tps   .com   find   tca   binary   code   

原文地址:https://www.cnblogs.com/HisonSanDiego/p/8296154.html

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