LintCode Binary Tree Maximum Path Sum
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.
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
class ResultType{
int a2a;
int r2a;
ResultType (int p1, int p2){
this.a2a = p1;
this.r2a = p2;
}
}
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int maxPathSum(TreeNode root) {
// write your code here
ResultType result = helper(root);
return result.a2a;
}
public ResultType helper(TreeNode root) {
if (root == null) {
return new ResultType(Integer.MIN_VALUE, Integer.MIN_VALUE);
}
ResultType left = helper(root.left);
ResultType right = helper(root.right);
int r2a = Math.max(0,Math.max(left.r2a,right.r2a)) + root.val;
int max = Math.max(left.a2a, right.a2a);
int a2a = Math.max(max, Math.max(left.r2a,0) + Math.max(right.r2a,0) + root.val);
return new ResultType(a2a, r2a);
}
}
LintCode Binary Tree Maximum Path Sum的更多相关文章
- [lintcode] Binary Tree Maximum Path Sum II
Given a binary tree, find the maximum path sum from root. The path may end at any node in the tree a ...
- [leetcode]Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 【leetcode】Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 26. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
- LeetCode: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- 【LeetCode】124. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
随机推荐
- Linux sticky bit 目录权限 rwt权限
[linux权限分为 user group others三组] 今天看到有个目录的权限是rwxrwxrwt 很惊讶这个t是什么,怎么不是x或者-呢?搜了下发现: 这个t代表是所谓的sticky bit ...
- Java开发中经典的小实例-(字符串比较)
//输入字符串然后与自己定义的数组进行对比,并输出重复次数. public class Test11 { public static void main(String[] args) { ...
- Entity Framework 教程
http://www.cnblogs.com/lsxqw2004/category/266012.html
- ie7 父元素宽度自适应且为浮动的话 子元素的宽度将不能按比例设置问题
好久没切图,昨天遇到个浏览器兼容的老问题,在ie7下,父元素设置浮动后,其宽度是自适应的,子元素的宽度若没有确定则将显示最小宽度,即文本所占的宽度. 正常其他浏览器显示如下: ie7中显示效果如下: ...
- jquery mobile的事件
有个问题困扰了我两天,知道今天才解决. 那就是page的pagecreate事件,只调用一次 如果想随时更新,就要调用pageshow事件,每次都会调用, 这个事情再次告诉我,基础要扎实啊,不然会浪费 ...
- java complier compliance level问题引发的思考
http://blog.csdn.net/shan9liang/article/details/17266519 ******************************************* ...
- git: windows git ssh keys生成
http://blog.csdn.net/lsyz0021/article/details/52064829 当我们使用github或者bitbucket等仓库时我们有可能需要ssh认证,所以需要生成 ...
- update
update `表名` set 字段名 =replace(字段名, '查找的内容','更改的内容') where 字段名 like '%查找的内容%'; update shangpin set cli ...
- C#MongoDB使用实践
9.5更新:更方便的启动命令 1)在D:\MongoDB中新建mongo.config文件,内容为 #启动mongod:mongod.exe --bind_ip 127.0.0.1 --logpath ...
- wireshark使用简介
wireshark界面简介 Wireshark是世界上最流行的网络分析工具.这个强大的工具可以捕捉网络中的数据,并为用户提供关于网络和上层协议的各种信息.与很多其他网络工具一样,Wireshark也使 ...