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

Leetcode 124*

时间:2019-02-09 13:30:15      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:bin   矛盾   style   etc   res   binary   单节点   col   ini   

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxPathSum(TreeNode* root) {
        int res = INT_MIN;
        DFS(root,res);
        return res;
    }
    int DFS(TreeNode* root,int &res){
        if(!root) return 0;
        int left = max(DFS(root->left,res),0);
        int right = max(DFS(root->right,res),0);
        res = max(res,left+right+root->val);
        return max(left,right)+root->val; //res是整个树的最大值,return 的是作为单节点的最大值,不矛盾
    }
};

_

Leetcode 124*

标签:bin   矛盾   style   etc   res   binary   单节点   col   ini   

原文地址:https://www.cnblogs.com/cunyusup/p/10357280.html

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