二叉树篇
easy.
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {private int ret=Integer.MIN_VALUE;public int maxPathSum(TreeNode root) {dfs(root);return ret;}private int dfs(TreeNode node){if(node==null){return 0;}int lVal=dfs(node.left);int rVal=dfs(node.right);ret=Math.max(ret,lVal+rVal+node.val);return Math.max(Math.max(lVal,rVal)+node.val,0);}
}