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

class Solution {
private:
    int res = INT_MIN;
public:
    /**
     * 
     * @param root TreeNode类 
     * @return int整型
     */
    int maxPathSum(TreeNode* root) {
        // write code here
        cal(root);
        return res;
    }
    
    int cal(TreeNode* root){
        if(root == nullptr){
            return 0;
        }
        
        int max_left = cal(root->left);
        int max_right = cal(root->right);
        
        int cur_res = max(max_left + root->val, max_right + root->val);
        cur_res = max(max_left + max_right + root->val, cur_res);
        cur_res = max(root->val, cur_res);
        res = max(cur_res, res);
        
        return max(max(max_left + root->val, max_right + root->val), root->val);
    }
    
};