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. STRTOK函数和STRTOK_R函数

    STRTOK函数和STRTOK_R函数 注:本文转载自博客园,感谢作者整理! 1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct pe ...

  2. linux之间文件传输(之scp)

    linux的scp命令 linux 的 scp 命令 可以 在 linux 之间复制 文件 和 目录: ==================scp 命令==================scp 可以 ...

  3. mybatis自定义插件动态修改sql语句

    step1:定义Interceptor实现org.apache.ibatis.plugin.Interceptor import org.apache.commons.logging.Log; imp ...

  4. Swift 类型转换

    1.类型转换 1.1 隐式类型转换 如 C 语言的类型转换 1.2 显式类型转换 Swift 语言是一种强类型语言,其整型的强制类型转换就是调用了参数类型对应的整形扩展构造方法,然后通过对应扩展构造方 ...

  5. 【java基础】Java反射机制

    一.预先需要掌握的知识(java虚拟机)  1)java虚拟机的方法区:  java虚拟机有一个运行时数据区,这个数据区又被分为方法区,堆区和栈区,我们这里需要了解的主要是方法区.方法区的主要作用是存 ...

  6. U盘启动装完系统后 一拔下优盘 就不能进入系统

    PE下Ghost安装的,装好进入系统正常,可是拔下 u盘就进不去系统,而插上 u盘就好好的 原因:引导的事,找到本地硬盘第一分区并且激活! 就可以了! 可用下“电脑店-修复主引导记录(MBR)工具”h ...

  7. ORA-01917: user or role 'PDB_DBA' does not exist

    在使用seed PDB创建新的PDB的时候,报了以下错误提示: SQL> create pluggable database pdb2 admin user admin1 identified ...

  8. 《跟老男孩学Linux运维:Web集群实战》读书笔记

    Linux 介绍 Linux 安装 Linux 调优 Web 基础 Nginx 应用 LNMP 应用 PHP 缓存加速 Nginx 调优 MySQL 应用 NFS 网络文件共享 Nginx 反向代理与 ...

  9. Win8.1开机自启动程序

    动机: 开机自启动goagent与锐捷 问题: goagent没有自启动选项;锐捷开启"登录后启动"却一直没有启动 解决方案: 使用计划任务启动goagent+彻底关闭UAC使锐捷 ...

  10. php分享十九:网络带宽预估

    网络带宽是指在一个固定的时间内(1秒),能通过的最大位数据.就好象高速公路的车道一样,带宽越大,好比车道越多. 数字信息流的基本单位是bit(比特),时间的基本单位是s(秒),因此bit/s(比特/秒 ...