题目:

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. Struts2学习五----------指定多个配置文件

    © 版权声明:本文为博主原创文章,转载请注明出处 指定多个配置文件 - 在Struts2配置文件中使用include可指定多个配置文件 实例 1.项目结构 2.pom.xml <project ...

  2. 通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。

    错误原因: xml文件中,本来是要配置成下面这样的: http://www.springframework.org/schema/context http://www.springframework. ...

  3. PowerBuilder -- Tab控件

    在tab中关闭窗口 Close(tab_1.getparent()) 调整tab中的控件的tab oder 鼠标右键tabpage_1,选择 Tab Order菜单.

  4. 离线安装Cloudera Manager5.3.4与CDH5.3.4(一)

    这几天一直在安装CDH,头都搞大了,安装第三次,最终成功了. 第一次问题非常多.后面卸载了.由于没有卸载干净导致第二次安装失败. 后来索性重装系统了.直接使用了纯净系统进行安装.一个人跑到学院机房去装 ...

  5. GS与网络打交道

    与网络打交道 在GS,GC,Share都与网络打交道,但还是GC最多 GC打交道过程 send_stat BaseChannel::SendCmdTry() { if (!m_queCmd.size( ...

  6. ABAP-创建货源清单

    CALL FUNCTION 'ME_DIRECT_INPUT_SOURCE_LIST' *&-------------------------------------------------- ...

  7. re.sub用法

    re.sub功能是对于一个输入的字符串,利用正则表达式,来实现字符串替换处理的功能返回处理后的字符串 re.sub共有五个参数 三个必选参数pattern,repl,string 两个可选参数coun ...

  8. selenium WebDriverException: Message: unknown error: DevToolsActivePort file doesnt exist

    在centos中使用无头chrome报以下错误 selenium.common.exceptions.WebDriverException: Message: unknown error: DevTo ...

  9. [2018-05-27]配置VSTS认证方式使用Personal Access Token

    本文介绍下如何配置VSTS(visual studio team service,其实就是微软SaaS版的TFS)通过Personal Access Token访问其下的Git代码库. 问题 使用gi ...

  10. LeetCode:寻找旋转排序数组中的最小值【153】

    LeetCode:寻找旋转排序数组中的最小值[153] 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0 ...