码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode Binary Tree Maximum Path Sum

时间:2016-08-11 06:20:25      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

Given a binary tree, find the maximum path sum.

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

Example

Given the below binary tree:

  1
 / 2   3

return 6.

For this problem we need to think about the problem in this way. Now we want the largest sum of all the potential path from one node to another node. For each node, there is two sub tree in left and right children. So by using divide and conqure method we could divide the problem into two subproblems. The most important issue is the relationship between problem of root node tree and two sub problems. We can define two variable, one is the sum of path from root to any node of its decendants. The other variable is the sum of length from any node to any node in the whole tree have processed.

For encapsulation, I created a new data structure ResultType which contains two integers stand for a2a and r2a. For each time after get two sub problems result we have a2a and r2a for two sub trees. Then for the tree of root node, we need assign largest value of two r2a values plus the root value. Because the r2a value is definately passing the root node so we can decide if we want to include the r2a value of two sub problems result. If any of them is less than 0, this means adding that to the node root2any will reduce the value then we just choose 0 instead of them which will only use the root.val. And for the a2a of root node, firstly, we should compare two a2a got from the two subproblems in left and right tree then choose the largest one. This means, there could be three potential largest values, which are, only in left tree, only in right tree and the path across left and right. However, we need to compare and choose the largest one.

 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     class ResultType{
14         int a2a;
15         int r2a;
16         ResultType (int p1, int p2){
17             this.a2a = p1;
18             this.r2a = p2;
19         }
20     }
21     /**
22      * @param root: The root of binary tree.
23      * @return: An integer.
24      */
25     public int maxPathSum(TreeNode root) {
26         // write your code here
27         ResultType result = helper(root);
28         return result.a2a;
29     }
30     public ResultType helper(TreeNode root) {
31         if (root == null) {
32             return new ResultType(Integer.MIN_VALUE, Integer.MIN_VALUE);
33         }
34         ResultType left = helper(root.left);
35         ResultType right = helper(root.right);
36         int r2a = Math.max(0,Math.max(left.r2a,right.r2a)) + root.val;
37         int max = Math.max(left.a2a, right.a2a);
38         int a2a = Math.max(max, Math.max(left.r2a,0) + Math.max(right.r2a,0) + root.val);
39         return new ResultType(a2a, r2a);
40     }
41 }

 

LintCode Binary Tree Maximum Path Sum

标签:

原文地址:http://www.cnblogs.com/ly91417/p/5759490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!