​https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/​LeetCode   124.二叉树的最大路径和_结点

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int max = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {

findMaxPath(root);
return max;
}
public int findMaxPath(TreeNode root) {
if(root==null){
//结点为空,那么最大权值是0
return 0;
}
//求左右子树的最大权值,与0比较是为了舍弃负值
int lMax = Math.max(findMaxPath(root.left),0);
int rMax = Math.max(findMaxPath(root.right),0);
//比较包括根结点的最大路径值
max = Math.max(max,lMax+rMax+root.val);

return root.val+Math.max(lMax,rMax);
}
}

LeetCode   124.二叉树的最大路径和_权值_02