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. HTML的 <u> 标签

    实例 使用 <u> 标签为文本添加下划线: <p>如果文本不是超链接,就不要<u>对其使用下划线</u>.</p> 亲自试一试 浏览器支持 ...

  2. js回车动态添加表格,右键动态删除表格行

    <script type="text/javascript" language="javascript">//屏蔽浏览器右键function sto ...

  3. hdu 1002

    ps:wa了好多次,然后才发现是输入的时候%s和%s要隔开一个空格,我想当然了... 代码: #include "stdio.h" #include "string.h& ...

  4. 头疼--windows之安装meteor.js

    如果你的电脑是window,这篇文章会的对你有些帮助. 进入meteor官网下载的meteor for windows安装包老是安装失败而且很慢,很慢,经过一番研究之下,终于安装成功了,特此来分享下经 ...

  5. 1.6jdk + eclipse + pydev搭建Python开发环境

    直接在1.6jdk的eclipse上用install new software的方法安装插件,会找不到安装好的插件.pydev官网还提供一种zip直接解压插件到eclipse文件夹下的dropins文 ...

  6. 使用 IDEA + Maven + Git 快速开发 JAVA或者Web 应用(转)

    0-0 前言 最近和同事做爬虫,其中我主要遇到的问题是:同事在github上放了爬虫demo让我自己去下载,然后自己能搭好环境让整个项目跑起来去抓51job找工作数据.git上克隆一个项目下来,项目是 ...

  7. 错误:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    我们在利用Eclipse进行Java web开发时,可能会出现这样的错误:The superclass javax.servlet.http.HttpServlet was not found on ...

  8. C# Substring的用法

    方法1  Substring(Int32) 从此实例检索子字符串. 子字符串在指定的字符位置开始并一直到该字符串的末尾. 方法2 Substring(Int32, Int32) 从此实例检索子字符串. ...

  9. codeforces 723E (欧拉回路)

    Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...

  10. C# xpath

    XPath最通俗的教程(ZZ)   以下是本人找到的最完整最易懂的XPath教程,不敢私藏,拿出来与大家分享.帮我点旁边的google广告呀. 实例 1基本的XPath语法类似于在一个文件系统中定位文 ...