首页 > 其他 > 详细

Binary Tree Maximum Path Sum

时间:2014-02-17 16:29:13      阅读:367      评论:0      收藏:0      [点我收藏+]

Given a binary tree, find the maximum path sum.

The path may start and end at any node in the tree.

For example:
Given the below binary tree,

       1
      /      2   3 Return 6.
bubuko.com,布布扣
 1 public class Solution {
 2     public int maxPathSum(TreeNode root) {
 3         int res []={Integer.MIN_VALUE};
 4         helper(root,res);
 5         return res[0];
 6     }
 7     public int helper(TreeNode root,int[] res){
 8         if(root==null) return 0;
 9         int left = helper(root.left,res);
10         int right = helper(root.right,res);
11         int across = root.val+left+right;
12         int single = Math.max(root.val,Math.max(root.val+right,root.val+left));
13         res[0] = Math.max(res[0],Math.max(across,single));
14         return single;
15     }
16 }
View Code

Binary Tree Maximum Path Sum

原文:http://www.cnblogs.com/krunning/p/3552029.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!