题目:

Given a binary tree, find the maximum path sum.

The path may start and end at any node in the tree.

Example

Given the below binary tree:

  1
/ \
2 3

return 6.

题解:

Solution 1 ()  from here

class Solution {
public:
int maxPathSum(TreeNode *root) {
if (root == NULL) {
return ;
} int res = INT_MIN;
helper(root, res); return res;
} int helper(TreeNode *root, int &res) {
if (root == NULL) {
return ;
} int sum = root->val;
int leftMax = helper(root->left, res);
int rightMax = helper(root->right, res); if (leftMax > ) {
sum += leftMax;
}
if (rightMax > ) {
sum += rightMax;
} res = max(sum, res); return max(, max(leftMax, rightMax)) + root->val;
}
};

【Lintcode】094.Binary Tree Maximum Path Sum的更多相关文章

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

  2. 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

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

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

  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 SumGiven a binary tree, find the maximum path sum. The path may start and e ...

  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. ROR部署到Heroku出现Application Error和code=H10 desc="App crashed“问题

    1.问题发现之前的准备 在读<Learn Python In Hard Way>的时候,发现作者谈到一个非常有趣的事情,在做一些有趣的事情之前做的无聊的事情叫做yak shaving,牦牛 ...

  2. Android推断是否有sd卡

    推断手机上是否有SD卡存在.作为经常用法,写到工具类里,用时直接调用.代码例如以下: public static boolean hasSdcard(){ String state = Environ ...

  3. C#与Java在修饰符上的不同

    1.readonly 修饰符仅用于修饰类的数据成员.正如其名字说的,一旦它们已经进行了写操作.直接初始化或在构造函数中对其进行了赋值,数据成员就只能对其进行读取. readonly 和 const 数 ...

  4. java eclipse使用不同jdk版本

    因为开发需要,两个工程分别需要使用jdk1.6(elipse indigo)和jdk1.8(eclipse neon).因为两个eclipse对于jdk版本的要求不同,若只在环境变量中配置jdk版本, ...

  5. 模式识别之基础---mqdf分类器==MQDF改进的二次分类器

    QDF假设样本符合高斯分布,通过估计均值与协方差矩阵,训练分类器.但是由于特征维数较高,时空复杂度较高.(协方差矩阵的维数为 特征维数*特征维数).而且协方差矩阵往往存在不满秩无法求逆的情况(样本数& ...

  6. Android NDK开发初步

    在配置好NDK开发之后就能够使用C/C++开发android了.以下以一个HelloWorld项目来说明 1.新建一个Androidproject 新建一个HelloWorldproject 代码例如 ...

  7. smarty模板里实现缓存。

    smarty模板里实现缓存.分页缓存在任何里都可以用 我用了三个类 include("../init.inc.php");//模板入口类 include("../DBDA ...

  8. 【BZOJ4383】[POI2015]Pustynia 线段树优化建图

    [BZOJ4383][POI2015]Pustynia Description 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息,每条信息包含三个数l,r ...

  9. 九度OJ 1011:最大连续子序列 (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5615 解决:2668 题目描述:     给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, N ...

  10. 九度OJ 1083:特殊乘法 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4114 解决:2809 题目描述: 写个算法,对2个小于1000000000的输入,求结果. 特殊乘法举例:123 * 45 = 1*4 + ...