问题

  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 

  Return6.

思路

  用递归方法从叶节点开始,将所求最大路径和maxValue设为全局变量,并赋初始值。

  假设递归到节点n,首先计算左子树的最大路径和left,并与0进行比较,若left<0,则left=0。同理求出右子树最大路径和right。

  将maxValue与left+right+root.val比较,把较大值赋于maxValue。

  递归函数的函数值为节点n.val与左右子树中较大值的和。

代码

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxValue = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) {
if(root==null)
return 0;
max(root);
return maxValue;
}
public int max(TreeNode root){
if(root==null)
return 0;
int left = Math.max(0, max(root.left));
int right = Math.max(0, max(root.right));
maxValue = Math.max(maxValue, left+right+root.val);
return Math.max(left, right)+root.val;
}
}

树——binary-tree-maximum-path-sum(二叉树最大路径和)的更多相关文章

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

  2. LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)

    题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...

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

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

  5. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

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

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

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

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

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

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

随机推荐

  1. 修改springboot控制台输出的图案

    原本启动springboot项目的日志是这样的: 但是我喜欢看见自己的名字,于是: 1.在src\main\resources文件夹下新建banner.txt 2.登录网站  patorjk.com/ ...

  2. 解决异常信息 Caused by: java.lang.IllegalArgumentException: invalid comparison: java.lang.String and java.util.Date

    原来的xml文件 <if test="null != endDate and '' != endDate"> AND rr.REG_DATE <= #{endDa ...

  3. HDU6030 Happy Necklace(推导+矩阵快速幂)

    HDU6030 Happy Necklace 推导或者可以找规律有公式:\(f[n] = f[n-1] + f[n-3]\) . 构造矩阵乘法: \[ \begin{pmatrix} f_i \\ f ...

  4. kubernetes-traefik(二十一)

    参考文档:http://traefik.cn/ traefik和ingress的对比 ingress: 使用nginx作为前端负载均衡,通过ingress controller不断的和kubernet ...

  5. 解决Firefox已阻止运行早期版本Adobe Flash

      解决Firefox已阻止运行早期版本Adobe Flash     类别    [随笔分类]web   解决Firefox已阻止运行早期版本Adobe Flash   最近火狐浏览器不知抽什么风, ...

  6. PROD异机全备份恢复验证实施文档

    PROD异机全备份恢复验证实施文档 ******利用10月25日的全量备份.10月26日当天的归档日志及当前redo日志,恢复数据库到故障前数据库状态****** 准备工作:source 源库:PRO ...

  7. C# 内存建表备忘

    #region=====建表===== DataSet dataSet; // 创建表 DataTable table = new DataTable("testTable"); ...

  8. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_01 Collection集合_5_迭代器的代码实现

    迭代器的类型和collection一样.都是String类型的 判断集合内是不是有元素 取出第一个元素 多次next获取所有的值 没有元素,再去取就会抛出异常. 适应while for循环的格式了解一 ...

  9. 05.vue-resource的基本使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. PHP 中的 $this, static , self ,parent 等等关键字的总结

    先说结论: self 和 __CLASS__,都是对当前类的静态引用,取决于定义当前方法所在的类.也就是说,self 写在哪个类里面, 它引用的就是谁.$this 指向的是实际调用时的对象,也就是说, ...