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.


对于树,我们可以找到其左右子树中终止于根节点的最大路径值,称为最大半路径值,如果都是正数,则可以把它们以及根节点值相加,如果其中有负数,则舍弃那一边的值(即置为零)。如此可以找到包含根节点的最大路径值。然后选择左右子树的最大半路径值中大的那个(负数则置为0)加上根节点的值作为新的最大半路径值传给父节点。

于是我们可以递归地考察每个子树,不断更新一个全局变量max。

基本情况为:子树为null,此时最大路径值应为0.

代码如下:

     int max;
public int maxPathSum(TreeNode root) {
max = root==null?0:root.val;
findMax(root);
return max;
}
public int findMax(TreeNode root) {
if(root==null)
return 0;
int left = Math.max(findMax(root.left),0);
int right = Math.max(findMax(root.right),0);
max = Math.max(max, left+right+root.val);
return Math.max(left, right) + root.val;
}

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

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

  2. leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)

    124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...

  3. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

  4. LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)

    题目: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as ...

  5. Java for LeetCode 124 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 124. Binary Tree Maximum Path Sum ----- java

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

  7. leetcode@ [124] Binary Tree Maximum Path Sum (DFS)

    https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...

  8. [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  9. [LeetCode] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

随机推荐

  1. oracle PL/SQL管理命令语句

    一.ORACLE的启动和关闭 1.在单机环境下 要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下 su - oracle a.启动ORACLE系统 oracle>svrmgrl ...

  2. google快捷键,通过浏览器本身来查看

    今天玩google浏览器时发现一个不需要访问google就能查看其快捷键的方式,再此记录一下,以备后用. 1:打开Google浏览器 2:打开开发者工具,有三种方法 2-1:按F12快捷键 2-2:按 ...

  3. PowerDesigner15中定义varbinary(max)列

    PowerDesigner15 概念数据模型(Entity)中要定义数据类型为varbinary(max)的特性(Attribute),应将数据类型(Data Type)选择为other,在代码(Co ...

  4. DIOCP之EchoServer分析

    constructor TfrmMain.Create(AOwner: TComponent);begin inherited Create(AOwner); sfLogger.setAppender ...

  5. 循序渐进Python3(一)-- 初识Python

    一.Python起源                

  6. 如何异步创建文件夹(node)

    模块fs作为node的核心模块之一,支持本地文件操作的接口,几乎对于所有的操作都提供同步和异步两种方案.例如,创建文件夹有mkdir和mkdirSync.不论对于mkdir还是mkdirSync,都需 ...

  7. swift 如何获取webView的内容高度

    应用中如果使用webView,要想获取其内容高度,就要实现其代理方法, 首先添加代理UIWebViewDelegate 然后给代理赋值 webView.delegate = self 实现代理方法: ...

  8. 重签名问题:does not have a signature matching

    今天在家里电脑重签名过的apk拿到公司来用装到模拟器上,运行Robotium测试用例时,报了如下错误,原本以为是工程里的activity名称和包名写错了呢,检查了一遍发现木有错误呀.... 好吧,那我 ...

  9. Daily Scrum 12.5

    今日完成任务: 对webservice进行学习,并将部分接口封装到webservice,没做完,明天继续.以便安卓组能够调用webservice的接口. 修复了大部分数据库改动后导致的异常,网站已可以 ...

  10. serialVersionUID的作用 (zz)

    serialVersionUID的作用 2011-05-12 16:04:19|  分类: java|举报|字号 订阅     在很多应用中,需要对某些对象进行序列化,让它们离开内存空间,入住物理硬盘 ...