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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 【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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 【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 ...

  8. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

  9. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

随机推荐

  1. qq2440启动linux后出现错误提示request_module: runaway loop modprobe binfmt-464c

    1.情景: 编译busybox时加了make CROSS_COMPILE=arm-linux-,但是还是出现了此情况! 2.解决方案如下: 配置busybox时,在配置中发现busybox setti ...

  2. XAF How to: 实现一个WCF Application Server 并配置它的客户端应用

    本主题描述了如何实现一个 WCF 中间层应用程序服务器及如何配置 XAF客户端连接到此服务器. 注意 本主题演示可以由解决方案向导自动生成的代码.执行操作时,如果你想要在现有的 XAF 解决方案中实现 ...

  3. seek指针大文件上传

    package mainimport (    // "bufio"    "fmt"    "github.com/axgle/mahonia&qu ...

  4. vs 2005 使用 boost regex

    第一步: Boost 入门及其VS2005下编译boost库  boost.regex库安装指南  深入浅出之正则表达式(一)  C++中三种正则表达式比较(C regex,C ++regex,boo ...

  5. neon指令,注意事项

    1. vbic_s8 (int8x8_t a, int8x8_t b) 是  ~(ai & bi),一开始理解成  (~ai )& bi 导致出错 2.uint8x8_t vqshrn ...

  6. Spring—Quartz定时调度CronTrigger时间配置格式说明与实例

    1 .CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 ...

  7. 在腾讯云上创建您的SQL Cluster(4)

    版权声明:本文由李斯达原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/255 来源:腾云阁 https://www.qclo ...

  8. 阿里云服务器 FTP配置图文教程和添加两个FTP站点

    1.添加FTP账号和密码. A. 选择“服务管理器”->“配置”->“本地用户和组”->“用户”:在空白处右键选择“新用户”: B. 输入用户名,全名和描述可以不填写:输入两遍密码: ...

  9. (35)odoo中widget

    widget大全: many2many_tagsone2many_listselectionprogressbarselectionstatusbarhandlemonetarymail_thread ...

  10. Gradient Boost Decision Tree(GBDT)中损失函数为什么是对数形式

    由于最近要经常用到XGBOOST的包,不免对相关的GBDT的原理又重新学习了一遍, 发现其中在考虑损失函数的时候,是以对数log进行度量的,囿于误差平方和函数的印象 那么为什么是对数呢?可能是下面的原 ...