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. 对于.NET Socket连接的细节记录

    如果客户端直接连接一个不存在的服务器端,客户端会抛出异常: 如果在连接过程中,客户端强制关闭了连接(没有调用Close直接关闭了程序),服务器端会抛出异常: 如果在连接过程中,客户端调用了Close, ...

  2. logsatsh input 插件之 collectd

    logsatsh input 插件之 collectd 标签(空格分隔): logstash 作用:用于监控内存,cpu,磁盘I等信息 未完待续,时间未定. 参考: logstash 官网 elast ...

  3. Power of Four(Difficulty: Easy)

    题目: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example ...

  4. OpenSSL 使用拾遗(二)---- X509 证书的 SKID/AKID 字段

    SKID(证书使用者密钥标识符,subject key identifier 的简称)和 AKID(证书颁发机构密钥标识符,authority key identifier 的简称)是 X509 证书 ...

  5. .net core 学习笔记(1)-分页控件的使用

    最近有个小项目,用.net core开发练练手,碰到的第一个问题就是分页控件的问题,自己写太费时间,上网查了下,发现有人已经封装好了的,就拿过来用了,分页控件github:https://github ...

  6. MySQL与Oracle的主要区别

    Mysql与Oracle区别 1. Oracle是大型数据库而Mysql是中小型数据库,Oracle市场占有率达40%,Mysql只有20%左右,同时Mysql是开源的而Oracle价格非常高. 2. ...

  7. Python学习路程day20

    本节内容: 项目:开发一个简单的BBS论坛 需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传 ...

  8. box-shadow中的理解(bootstrap)

    刚研究了bootstrap中css里面的源码,找到了表单(form)中关于输入框的一些设置,根据要求,label标签和input标签需要一起使用,(屏幕阅读器中不能单独辨认input),如需隐藏lab ...

  9. Android性能测试工具APT使用指南

    腾讯的安卓平台高效的性能测试工具APT(Android Performance Testing Tools),适用于开发自测和定位性能瓶颈,帮助测试人员完成性能基准测试.竞品测试. APT提供了CPU ...

  10. bootstraps字体图标无法显示

    使用bootstraps字体图标,必须在css的同级文件夹下,建立新的文件夹为fonts,放入一下文件. 在还是无法显示字体图标的情况下,可查看bootstraps.css中的 @font-face ...