题目:

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. Android使用ViewPager实现无限循环滑动及轮播(附源代码)

    MainActivity例如以下: package cc.ww; import java.util.ArrayList; import android.app.Activity; import and ...

  2. 解析域名得到IP

    本文转载至  http://www.cocoachina.com/bbs/read.php?tid=142713&page=e&#a   分享类型:游戏开发相关 #include &l ...

  3. python 基础 7.0 import 导入

    一. python 常用内置模块的使用(datetime,logging,os,command)       在日常的开发工作中,我们要写很多的python 代码,如果都写在一个文件中,会导致代码特别 ...

  4. T-SQL高级查询语句(父子查询)

    T-SQL高级查询语句 高级查询 1.连接查询,对结果集列的扩展 select * from info select * from info,nation #形成笛卡尔积 select * from ...

  5. Java 学习 day04

    17-数组(概述-内存结构) 概念:同一种类型数据的集合,其实数组就是一个容器. 可以自动给数组中的元素从0开始编号,方便操作这些元素. int[] x = new int[3]; 01-数组(静态初 ...

  6. EasyRTMP Android安卓手机直播推流摄像头偏暗的问题解决

    在我们测试EasyRTMP Android安卓手机推流的过程中发现有些设备预览时,明显偏暗!在稍微暗点的环境中几乎很难看清东西-额,这是怎么回事呢?又是安卓设备的兼容性问题,头疼! !!!好吧,停止抱 ...

  7. 通用分页(Jquery版)

    1.简单定义下样式 <style type="text/css"> .fanye { color: blue; margin-right: 15px; text-dec ...

  8. 九度OJ 1039:Zero-complexity Transposition(逆置) (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3093 解决:1255 题目描述: You are given a sequence of integer numbers. Zero-co ...

  9. await 暂停 等待 暂停的是什么

    体验异步的终极解决方案-ES7的Async/Await var sleep = function (time) { return new Promise(function (resolve, reje ...

  10. go html ecmascript

    <script> var go={{.}}</script> {{define "PotentialCustomer"}} <!DOCTYPE htm ...