Given a binary tree, find the maximum path sum from root.

The path may end at any node in the tree and contain at least one node in it.

给一棵二叉树,找出从根节点出发的路径中,和最大的一条。

这条路径可以在任何二叉树中的节点结束,但是必须包含至少一个点(也就是根了)。

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root the root of binary tree.
* @return an integer
*/
public int maxPathSum2(TreeNode root) {
if (root == null) {
return Integer.MIN_VALUE;
} int left = maxPathSum2(root.left);
int right = maxPathSum2(root.right); return root.val + Math.max(0, Math.max(left, right));
}
}

[lintcode] Binary Tree Maximum Path Sum II的更多相关文章

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

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

  4. 26. Binary Tree Maximum Path Sum

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

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

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

  7. 【LeetCode】124. 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 Sum Given a binary tree, find the maximum path sum. The path may start ...

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

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

随机推荐

  1. gnuplot使用3

    linetype set linetype命令允许用户重定义默认的显示线的类型,该命令的选项跟"set style line"是一样的.于"set style line& ...

  2. easyUI增加视图分组的办法

    1.在JSP头文件中引入如下代码 <script type="text/javascript" src="${pageContext.request.context ...

  3. Spring事务传播简介

    一.事务传播属性(propagation) 1.REQUIRED,默认属性 此级别下,会为每一个调用的方法创建一个逻辑事务域,如果前面的方法已经创建了事务,那么后面的方法支持当前事务,如果当前没有事务 ...

  4. easyUI datagrid editor扩展dialog

    easyUI datagrid简单使用:着重两点1.editor对象的click事件:2.将dialog窗体内的值填写到当前正编辑的单元格内 <!DOCTYPE html> <htm ...

  5. 10月21日上午MySQL数据库学习内容复习

    1.创建数据库create database 数据库名称删除数据库drop database 数据库名称 2.创建表create table 表名(列名 类型(长度) 自增长 主键 非空,)自增长:a ...

  6. ubuntu-E:Encountered a section with no Package: header的解决办法 (转)

    E:Encountered a section with no Package: header, E:Problem with MergeList /var/lib/apt/lists/cn.arch ...

  7. yourtour的几种链接

    php,html {:URL('User-Register/index')}    格式:http://www.xxx.com/index.php?g=User&m=User&a=in ...

  8. python 函数基础介绍

    函数是对程序逻辑进行结构化或过程化的一种编程方法.能将整块代码巧妙地隔离成易于管理的小块,把重复代码放在函数中而不是进行大量的拷贝. 一.函数创建 def 函数创建格式如下: def function ...

  9. Python 开发与测试 Webservice(SOAP)

    WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 理解WebService 1.从表面上看,WebService就是一个应用程序向外界暴露出一个能通过Web进行调用的API,也就是 ...

  10. Storm集群安装部署步骤【详细版】

    作者: 大圆那些事 | 文章可以转载,请以超链接形式标明文章原始出处和作者信息 网址: http://www.cnblogs.com/panfeng412/archive/2012/11/30/how ...