问题

  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. Android环境配置之正式版AndroidStudio1.0

    昨天看见 Android Studio 1.0 正式版本发布了:心里挺高兴的. 算是忠实用户了吧,从去年开发者大会一开始出现 AS 后就开始使用了:也是从那时开始就基本没有用过 Eclipse 了:一 ...

  2. Pap.er 模仿 - 第一天

    最后更新: 2017-12-15 一. 项目初始化 解析对应的资源, 下载Pap.er之后,需要解析里面的资源. 采用如下的方法: http://blog.csdn.net/xuzihai0703/a ...

  3. 使用vue技术应当使用的技术和兼容性选择

    假如你的前端框架使用了vue,那你可以大胆地使用以下技术,并忽略其他js和css的兼容性问题,因为 关于vue的兼容性 官方给出了规定 Vue 不支持 IE8 及以下版本,因为 Vue 使用了 IE8 ...

  4. 大数据笔记(四)——操作HDFS

    一.Web Console:端口50070 二.HDFS的命令行操作 (一)普通操作命令 HDFS 操作命令帮助信息: hdfs dfs + Enter键 常见命令 1.  -mkdir 在HDFS上 ...

  5. Linux中相关知识(atexit(),fork(),粘滞位)

    1.atexit()函数 函数名: atexit 头文件:#include<stdlib.h> 功 能: 注册终止函数(即main执行结束后调用的函数) 用 法: int atexit(v ...

  6. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_09 序列化流_5_InvalidClassException异常_原理

    序列化一遍 反序列化再来一遍 age的修饰符改为public 直接反序列化就会抛出异常 序列化的时候会给Person.class添加序列号,serialVersionUID,.反序列化需要对比这个se ...

  7. HAWQ技术总结

    HAWQ技术总结: 1. 官网: http://hawq.incubator.apache.org/ 2. 特性 2.1 sql支持完善 ANSI SQL标准,OLAP扩展,标准JDBC/ODBC支持 ...

  8. 自动化测试--利用opencv进行图像识别与定位

    SIFT检测方法 SIFT算法就是把图像的特征检测出来,通过这些特征可以在众多的图片中找到相应的图片 import cv2 #读取图片,以1.png为例 img=cv2.imread('1.png') ...

  9. 流程控制: if分支 while循环 for循环

    流程控制 Python程序执行,一定按照某种规律在执行 1.宏观一定是自上而下(逻辑上方代码一定比逻辑下方代码先执行):顺序结构 2.遇到需要条件判断选择不同执行路线的执行方式:分支结构 3.有些事情 ...

  10. 剑指offer--day07

    1.1 题目:反转链表:输入一个链表,反转链表后,输出新链表的表头. 1.2 思路:这道题,我们要做到的是反转链表,我们的思路是将前一个节点与后一个节点断开,然后让后一个节点指向前一个节点,这个过程就 ...