https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/
这个题明明有思路,但是写出来的代码就是一团糟还过不了,一气之下直接放弃治疗看讨论区了。。
先贴代码
class Solution { int realMax = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { getMax(root); return realMax; } private int getMax(TreeNode root){ if(root == null){ return 0; } int left = Math.max(0,getMax(root.left)); int right = Math.max(0,getMax(root.right)); realMax = Math.max(realMax, root.val+left+right); return Math.max(left, right) + root.val; } }View Code
https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/comments/3015