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. SOA 和webservice 的区别

    http://blog.csdn.net/bingjing12345/article/details/7575566 Web service 的具体过程 需要明确的东西 1, 服务器端 和 客户端 之 ...

  2. PCA 主成分分析(Principal components analysis )

    问题 1. 比如拿到一个汽车的样本,里面既有以“千米/每小时”度量的最大速度特征,也有“英里/小时”的最大速度特征,显然这两个特征有一个多余. 2. 拿到一个数学系的本科生期末考试成绩单,里面有三列, ...

  3. WinForm------TreeList实现鼠标经过节点背景色改变

    转载: http://www.cnblogs.com/zfanlong1314/archive/2012/06/26/2564124.html

  4. android studio中ListView与SQLite的结合使用

    Da.java public class Db extends SQLiteOpenHelper { public Db(Context context) { super(context, " ...

  5. Jquery 的事件方法

    1.$(selector).bind(event,data,function,map) //给元素添加一个事件 2.当元素失去焦点时发生 blur 事件,获得焦点时触发focus事件: $(" ...

  6. Java Web之Servlet

    Servlet参考文献: 1.http://www.cnblogs.com/luoxn28/p/5460073.html 2.http://www.cnblogs.com/xdp-gacl/p/376 ...

  7. MagicalRecord简单使用小记

    一般采用pod安装,导入框架 #import <CoreData+MagicalRecord.h> - (BOOL)application:(UIApplication *)applica ...

  8. wordpress数据库表说明

    wp系统所用的表不多,那么每张表具体都存些什么?今天给大家介绍一下,希望对你有帮助. wp_commentmeta: 用于保存评论的元信息,在将评论放入回收站等操作时会将数据放入此表,Akismet等 ...

  9. centos 7.0 PHP 5.6.5 安装过程 (php+nginx)

    php网址 http://php.net/downloads.php 首先下载 php-5.6.5.tar.gz [root@localhost src]# wget http://cn2.php.n ...

  10. python抓取网站URL小工具

    1.安装Python requests模块(通过pip): 环境搭建好了! 2.测试一下抓取URL的过程: 抓取出来的URL有JavaScript代码,正则上还有待更加完善,有兴趣的可以研究下~! 工 ...