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.

For example:
Given the below binary tree,

       1
/ \
2 3

Return 6.

递归求解。

maxPathSum(root)跟maxPathSum(root.left)和maxPathSum(root.right)之间的关系:

root左子树的maxPath,右子树的maxPath以及根节点之间无法建立直接递归关系。也就是说以下的递推式不成立:

maxPathSum(root) = max{ maxPathSum(root.left), maxPathSum(root.right), maxPathSum(root.left) + maxPathSum(root.right) + root.val }

然而,按照动态规划的思路,root的结果跟其左右子树的结果之间应该是存在递推关系的:

maxPathSum(root) = F( maxPathSum(root.left), maxPathSum(root.right), root )

只是,这个root节点加进来之后如何影响最优解?进一步梳理思路:

F( maxPathSum(root.left), maxPathSum(root.right), root )

/ max{maxPathSum(root.left), maxPathSum(root.right)},                                                                   if root 将不包含在最长路径中

=  {

\ max{maxPathSum(root.left), maxPathSum(root.right), max path sum of the path includes root},    if root 将包含在最长路径中

问题将归结为:求出一条包含root节点的最长路径,并比较该路径的长度与其左右子树的最长路径长度。

所以,在递归过程中,我们需要计算两个值:

1)包含节点在内的最长路径(只可能跟该节点的左子树或者右子树相关);

2)该节点作为根节点的子树的最长路径和;

定义class描述这个递归中间结果:

class max_val {
int max_path_include_root_half_tree; // 辅助值
int max_path_sum; // 待求解值 public void set(int x) {
max_path_include_root_half_tree = max_path_sum = x;
}
}

递归过程:

private void maxPathSum(TreeNode root, max_val max_vs) {
if (root == null) {
max_vs.set(-2147483647 >> 2);
return;
} if (root.left == null && root.right == null) {
max_vs.set(root.val);
return;
} max_val left_ = new max_val();
maxPathSum(root.left, left_); max_val right_ = new max_val();
maxPathSum(root.right, right_); int a = left_.max_path_include_root_half_tree + root.val;
int b = right_.max_path_include_root_half_tree + root.val;
int c = root.val;//a,b,c中包含root的值,并且最多包含了左子树或者右子树,这类路径可用于组成包含父节点的路径
int d = a + right_.max_path_include_root_half_tree;
int f = left_.max_path_sum;
int g = right_.max_path_sum; max_vs.max_path_include_root_half_tree = max(new int[] { a, b, c });// 包含root的最长路径 max_vs.max_path_sum = max(new int[] { a, b, c, d, f, g });// root作为根节点的树的最长路径和 }

最终求解:

	public int maxPathSum(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
max_val mv_l = new max_val();
maxPathSum(root, mv_l); return mv_l.max_path_sum;
}

[leetcode]Binary Tree Maximum Path Sum的更多相关文章

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

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

  3. [LeetCode] 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. ...

  4. leetcode–Binary Tree Maximum Path Sum

    1.题目说明 Given a binary tree, find the maximum path sum.   The path may start and end at any node in t ...

  5. C++ leetcode Binary Tree Maximum Path Sum

    偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点. 题目: Given a binary tree, find the maximum path su ...

  6. [LeetCode] 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. ...

  7. [leetcode]Binary Tree Maximum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...

  8. [Leetcode] 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. ...

  9. LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)

    题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...

随机推荐

  1. Oracle中SQL查询表字段基本信息、主键、外键(转)

    select utc.column_name, utc.data_type, utc.data_length, utc.data_precision, utc.data_Scale, utc.null ...

  2. 作业七:团队项目——Alpha版本冲刺阶段003

    今日进展:我们的目标是做一款扫雷游戏,所以我们先去玩了几款游戏,找到了扫雷游戏的一些特点. 今日安排:先进行了一些必要的游戏过程,进行了基本的扫雷界面规划.

  3. Android高效加载大图、多图解决方案,有效避免程序OOM

    高效加载大图片 我们在编写Android程序的时候经常要用到许多图片,不同图片总是会有不同的形状.不同的大小,但在大多数情况下,这些图片都会大于我们程序所需要的大小.比如说系统图片库里展示的图片大都是 ...

  4. Linq中关键字的作用及用法

    Linq中关键字的作用及用法 1.All:确定序列中的所有元素是否都满足条件.如果源序列中的每个元素都通过指定谓词中的测试,或者序列为空,则为 true:否则为 false. Demo: 此示例使用 ...

  5. Selenium之(二)Junit单元测试框架

    书目-selenium 实战宝典 章节:第七章 p63-73 1.被测程序 2.测试代码 3.多个测试类整合到一起 4.运行查看结果

  6. java获取日期 昨天 今天 明天的日期

    Date date=new Date();//取时间 Calendar calendar = new GregorianCalendar(); calendar.setTime(date); cale ...

  7. html canvas 骰子1

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 普通硬件就能破解GSM A5加密算法

    如果你还在使用基于早期信号标准的GSM手机,那么你最好在使用手机的过程中小心一点了.因为根据国外媒体的最新报道,安全研究专家通过测试发现,他们只需要使用三张NVIDIA GeForce GTX690显 ...

  9. 微软How old do I Look——初体验

    前段时间微软发布了一个可爱的网站how old.net,着实火了一把,全民体验魔镜魅力. 上传自己的靓照到http://www.how-old.net/,它就可以告诉你性别和年龄,大家还习惯称之为“颜 ...

  10. 二叉树的实现与一些基本操作(C++环境)

    #include<cstdio>#include<cstdlib>#include<iostream>#include<cstring>using na ...