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

class Solution {
public:
    /**
     * 
     * @param root TreeNode类 
     * @return int整型
     */
    int res = INT32_MIN;
    int dfs(TreeNode* root){
        if (root == NULL) {
            return 0;
        }
        if (root->left == NULL && root->right == NULL) {
            res = max(res, root->val); //判断叶子节点
            return root->val;
        }
        int leftValue = dfs(root->left);
        int rightValue = dfs(root->right);
        int maxv = max(leftValue + root->val, rightValue +  root->val);
        res = max(res, maxv);//判断左/右节点 + 当前节点
        res = max(res, root->val);//判断遍历的当前节点
        res = max(res, leftValue + rightValue + root->val);//判断左节点 + 右节点 + 当前节点
        return maxv;
    }
    
    int maxPathSum(TreeNode* root) {
        res = INT32_MIN;
        dfs(root);
        return res;
    }
};