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 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
随机推荐
- 关于Java项目打包
可以选择以下几种办法: 一.使用Eclipse,右键项目导出jar. 二.使用Eclipse,右键项目导出runnable jar. 三.使用Eclipse 插件fat jar,导出可执行的jar包. ...
- C++11在时空性能方面的改进
C++11在时空性能方面的改进 这篇我们聊聊C++11在时间和空间上的改进点: 主要包括以下方面: 新增的高效容器:array.forward_list以及unordered containers: ...
- IOS开发数据库篇—SQLite模糊查询
IOS开发数据库篇—SQLite模糊查询 一.示例 说明:本文简单示例了SQLite的模糊查询 1.新建一个继承自NSObject的模型 该类中的代码: // // YYPerson.h // 03- ...
- SQL Server索引调优系列
http://www.cnblogs.com/chenmh/category/586612.html http://www.cnblogs.com/zhijianliutang/category/63 ...
- 1074, "Column length too big for column 'err_solution' (max = 21845); use BLOB or TEXT instead"
一个注意点,就是sqlalchemy 使用create_all()建表的时候,要在 create_all()所在页面import那些表的model sqlalchemy.exc.Operational ...
- Ubuntu安装SSH服务
1 SSH服务 Ubuntu默认并没有安装ssh服务,如果通过ssh远程连接到Ubuntu,需要自己手动安装ssh-server(openssh-server). 1.1 检测是否安装SSH服务 出现 ...
- Ubuntu中设置静态IP和DNS
在Ubuntu中设置静态IP共两步:1>设置IP:2>设置DNS1>设置IP 编辑 /etc/network/interface文件: sudo vi /etc/n ...
- 通过一行代码学习javascript
[].forEach.call($$("*"), function (a){ a.style.outline = "1px solid #"+(~~(Math. ...
- sass学习笔记2
今天介绍sass在重用代码时最具威力的两个功能.一个是嵌套(Nesting),一个混合(Mixin). 我们在写CSS通过需要多个后代选择器组合到一起才能定位到目标元素上,而这定义过程,此元素的父元素 ...
- Octopus系列之一些问题的汇总
1._WidgetCart.html中的数量更新函数和删除函数,转移到MyShoppingCart.html中出错 如果你使用的是jQuery的方式banding函数 比如change 和click, ...