LEETCODE : binary-tree-maximum-path-sum

LEETCODE : binary-tree-maximum-path-sum_第1张图片 

/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param root TreeNode类 
     * @return int整型
     */
    int maxV = INT_MIN;
    int maxPathSum(TreeNode* root) {
        // write code here
        if(NULL == root) return 0;
        helpFun(root);
        return maxV;
    }
    
    int helpFun(TreeNode* root){
        if(NULL == root) return 0;
        int left = helpFun(root->left);
        int right = helpFun(root->right);
        maxV = max(maxV,max(0,left)+root->val+max(0,right));
        return max(max(0,left)+root->val, max(0,right)+root->val);
    }
};

 

你可能感兴趣的:(leetcode)