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 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
随机推荐
- max-width实现图片的自适应
在自适应布局中,有时候会让图片随着宽度的变化相应的放大或者缩小,或者说让图片等比缩放,但是每个图片的大小又不一样,这个时候我们就要用到max-width这个属性了. img{ max-width:10 ...
- 将php网站移到CentOS 6.7上[二]:将网站部署到服务器上
首先,确保lamp环境已安装好.准备好项目源代码,数据库备份文件等.由于没有安装好VNC,因此只能用ssh部署了. 将项目源代码压缩,Linux默认是支持SFTP的,用SFTP将源代码压缩包上传到 / ...
- linux SVNUP显示无法连接主机
今天开发环境中突然无法连接主机了,我就想到 1.更改svn 链接地址,发现不可取,工程中的每个目录下面都有个.svn文件,修改起来麻烦: 2.建立新文件夹,重新checkout,发现还是无法链接 最后 ...
- postMessage
postMessage 父页面 e.target.contentWindow.postMessage(messageData, '*'); /*******onMessage处理******/ fun ...
- iOS 如何适配iOS10
转自: http://www.cnblogs.com/jukaiit/p/5881062.html 2016年9月7日,苹果发布iOS 10.2016年9月14日,全新的操作系统iOS 10将正式上线 ...
- mac 安装tomcat
一.下载 首先在tomcat官网下载完整版的 tomcat.tar.gz包,Core下 注: zip用于windows操作系统,tar.gz用于unix和linux操作系统. Binary Distr ...
- 火狐浏览器Firefox上DownThemAll插件
DownThemAll插件支持断点续传.多线程下载,可以大幅度提高下载速度. 在Windows平台上,要下载大量的文件,迅雷自然是首选:但在非Windows平台上,只要安装一个火狐浏览器,再安装Dow ...
- 微软2017校招笔试题3 registration day
题目 It's H University's Registration Day for new students. There are M offices in H University, numbe ...
- 查看本机的IP地址方法:
查看本机的IP地址方法:对于XP系统:方法一:如果右下角系统托盘区有本地连接的小电脑,双击小电脑→支持,就可以看到本机IP地址.无线连接也是一样.方法二:开始→运行cmd /k ipconfig,IP ...
- python3 登录接口
登录接口 功能: 输入用户名(有一个用户名及对应的密码表) 认证成功后显示欢迎信息 输错三次后锁定(即第四次提示该账户已被锁定)用户登录锁定记录写到一个文件中. 用到:自定义函数.列表.字典 #Au ...