public class Solution{
int maxValue;
public int maxPathSum(TreeNode root){
maxValue = Integer.MIN_VALUE;
maxPathDown(root);
return maxValue;
}
private int maxPathDown(TreeNode node){
if(node == null){
return 0;
}
int left = Math.max(0,maxPathDown(node.left));
int right = Math.max(0, maxPathDown(node.right));
maxValue = Math.max(maxValue, left + right + node.val);
return Math.max(left, right) + node.val;
}
}
124. Binary Tree Maximum Path Sum
最新推荐文章于 2022-06-17 14:44:33 发布