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

SOLUTION 1:

计算树的最长path有2种情况:

1. 通过根的path.

(1)如果左子树从左树根到任何一个Node的path大于零,可以链到root上

(2)如果右子树从右树根到任何一个Node的path大于零,可以链到root上

2. 不通过根的path. 这个可以取左子树及右子树的path的最大值。

所以创建一个inner class:

记录2个值:

1. 本树的最大path。

2. 本树从根节点出发到任何一个节点的最大path.

注意,当root == null,以上2个值都要置为Integer_MIN_VALUE; 因为没有节点可取的时候,是不存在solution的。以免干扰递归的计算

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public class ReturnType {
int maxSingle;
int max;
ReturnType (int maxSingle, int max) {
this.max = max;
this.maxSingle = maxSingle;
}
} public int maxPathSum(TreeNode root) {
return dfs(root).max;
} public ReturnType dfs(TreeNode root) {
ReturnType ret = new ReturnType(Integer.MIN_VALUE, Integer.MIN_VALUE);
if (root == null) {
return ret;
} ReturnType left = dfs(root.left);
ReturnType right = dfs(root.right); int cross = root.val; // if any of the path of left and right is below 0, don't add it.
cross += Math.max(0, left.maxSingle);
cross += Math.max(0, right.maxSingle); // 注意,这里不可以把Math.max(left.maxSingle, right.maxSingle) 与root.val加起来,
// 会有可能越界!
int maxSingle = Math.max(left.maxSingle, right.maxSingle); // may left.maxSingle and right.maxSingle are below 0
maxSingle = Math.max(maxSingle, 0);
maxSingle += root.val; ret.maxSingle = maxSingle;
ret.max = Math.max(right.max, left.max);
ret.max = Math.max(ret.max, cross); return ret;
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/MaxPathSum.java

LeetCode: Binary Tree Maximum Path Sum 解题报告的更多相关文章

  1. 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

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

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

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

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

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

  7. C++ leetcode Binary Tree Maximum Path Sum

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

  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求二叉树最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

随机推荐

  1. 理解iOS与函数式编程

    有时候,一个关键字就是一扇通往新世界的大门.两年前,身边开始有人讨论函数式编程,拿关键字Functional Programming一搜,全是新鲜的概念和知识,顺藤摸瓜,看到的技术文章和框架也越来越多 ...

  2. 有关memcached企业面试案例讲解

    有关memcached企业面试案例讲解 1.Memcached是什么,有什么作用?    a. memcached是一个开源的.高性能的内存的缓存软件,从名称上看Mem就是内存的意思,而Cache就是 ...

  3. 学习asp.net的流程

    如果你已经有较多的面向对象开发经验,跳过以下这两步: 第一步 掌握一门.NET面向对象语言,C#或VB.NET 我强烈反对在没系统学过一门面向对象(OO)语言的前提下去学ASP.NET. ASP.NE ...

  4. golang 学习笔记 ---make/copy/append

    package main import ( "fmt" ) func main() { a := [...]int{0, 1, 2, 3, 4, 5, 6, 7} s := mak ...

  5. SharePoint 2013 启用 以其他用户身份登陆(Sign in as different user)

    习惯于SharePoint 2010的用户会发现,SharePoint 2013默认把  以其他用户身份登陆(Sign in as different user)的选项去掉了,这对于开发人员来说很麻烦 ...

  6. dedecms {dede:php}标签用法介绍

    最简单的输入如 代码如下 复制代码 {dede:php} $numA = 1; $numB = 2; echo $numA + $numB; {/dede:php} 从上面语句可以看出dede:php ...

  7. mysql获取group by的总记录行数方法

    mysql获取group by内部可以获取到某字段的记录分组统计总数,而无法统计出分组的记录数. mysql的SQL_CALC_FOUND_ROWS 使用 获取查询的行数 在很多分页的程序中都这样写: ...

  8. 在Java中final类与一般类有什么样的区别

    final修饰的类不能被继承. Sting就是一个被final修饰的类,我们只能用,不用继承final不仅可以修饰类,还可以修饰变量,被final修饰的变量就是一个常量,只能赋值一次注意final和f ...

  9. LINQ中in的实现方法-LINQ To Entities如何实现查询 select * from tableA where id in (1,2,3,4)

    如果用in是字符串类型无问题,可以直接这样用 ).Where(entity => urls.Contains((entity.NavigateUrl == null ? "" ...

  10. yml配置文件

    1 yaml配置文件中是以空格来区分是否是同一层级.在键值对的冒号之后要有空格. 2 属性值:数值.字符串.bool 值,直接写即可.双引号会将特殊字符进行转义,例如:"nice \n go ...