124. Binary Tree Maximum Path Sum

 1 class Solution {
 2     int max = Integer.MIN_VALUE;
 3     public int maxPathSum(TreeNode root) {
 4         if(root == null) return 0;
 5         if(root.left == null && root.right == null) return root.val;
 6         int i = helper(root);
 7         return max;
 8         
 9     }
10     
11     public int helper(TreeNode root){
12         if(root == null) return 0;
13         int left = helper(root.left);
14         int right = helper(root.right);
15         int maxSingle = Math.max(Math.max(left, right), 0) + root.val;
16         max = Math.max(Math.max(max, maxSingle), (left+right)+root.val);
17         return maxSingle; //因为是path 所以只能返回最大的一边 不能全部返回
18     }
19 }

 

posted @ 2018-10-24 03:54  jasoncool1  阅读(95)  评论(0编辑  收藏  举报