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 EBS Java Applet报错:找不到类

    Oracle EBS Home Page可以打开,但是无法打开EBS的Form,查看Java控制台,有错误报出. java控制台报错,如下: Java Plug-in 1.6.0_07 使用 JRE ...

  2. 自动化测试selenium+java 第三章

    import java.sql.Time;import java.util.concurrent.TimeUnit; import org.junit.BeforeClass;import org.o ...

  3. 8.adr与ldr伪指令的区别

    ldr和adr都是伪指令,区别是ldr是长加载.adr是短加载. 重点:adr指令加载符号地址,加载的是运行时地址: ldr指令加载符号地址时,加载的是链接地址.

  4. 27、初步探索echarts源码

    1.首先发现随笔中凡是和echarts相关的点击率都特别高,于是乎就接着写了echarts因为感觉要转点击率 首先声明我并不是专业做前端的,所以如果有些说得不对的地方,希望前端大神们出来指正 首先发现 ...

  5. xtrabackup备份与恢复实践

    说明   xtrabackup  --percona 特点: 开源,在线备份innodb表 支持限速备份,避免对业务造成影响 支持流备 支持增量备份 支持备份文件压缩与加密 支持并行备份与恢复,速度快 ...

  6. XidianOJ 1172 Hiking

    题目描述 BlacKin and GKCY are going hiking together. Besides their personal items, there are some items ...

  7. asp.net GDI+绘制折线

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  8. css3的一些新属性1

    <body> /*文本阴影*/ <h1 style="text-shaow:5px 5px 5px #C0F">我爱你</h1> </bo ...

  9. iOS button 里边的 字体的 摆放

    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; button.titleEdgeInsets ...

  10. JavaScript小功能

    1. JS判断是否为一个有效日期 1 2 3 4 function check(date){     return (new Date(date).getDate()==date.substring( ...