题目:

Given a binary tree, find the maximum path sum.

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

For example:
Given the below binary tree,

       1
/ \
2 3

Return 6.

解题思路:

最长路径和要分几种情况考虑,对于这样一个节点

       cur
/ \
left right

设left是左子树的最长路径和,right是右子树的最长路径和,要求当前节点的最长路径和,只要将1、当前节点的val值2、当前节点val值加上left 3、当前节点val值加上rihgt,取其最大者作为当前节点的最长路径和。

以上我们只是考虑了三种情况,1、最长路径是从cur节点开始;2、最长路径从左边上来经过cur;3、最长路径从右边上来经过cur

还有三种情况需要考虑,1、如果left值是最长路径呢,即最长路径不经过cur,到left就为止了;2、如果right值是最长路径呢?3、最长路径经过cur,但是没有向上走,而是向另一个分支去了呢(left or right)?

所以我们需要定义一个全局参数,求出left,right,及left+right+cur值的最大值,最后与递归结束后过根节点的最长路径长度比较,取其大者为此题答案。

实现代码:

class Solution {
public:
int maxPathSum(TreeNode *root) {
if(root == NULL)
return 0;
int maxsum = INT_MIN;
int ret = getMax(root, maxsum);
return max(ret, maxsum); } int getMax(TreeNode *root, int &maxsum)
{
if(root == NULL)
return INT_MIN>>4;
int leftmax = getMax(root->left, maxsum);
int rightmax = getMax(root->right, maxsum);
maxsum = max(maxsum, max(max(leftmax, rightmax), leftmax + root->val + rightmax));
return max(root->val, max(leftmax, rightmax) + root->val); }
};

LeetCode124:Binary Tree Maximum Path Sum的更多相关文章

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

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

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

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

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

  7. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

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

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

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

  9. [Swift]LeetCode124. 二叉树中的最大路径和 | Binary Tree Maximum Path Sum

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

随机推荐

  1. 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求

    系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求  实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目   实战使 ...

  2. 关于QCon2015感想与反思

    QCon2015专场有不少关于架构优化.专项领域调优专题,但能系统性描述产品测试方向只有<携程无线App自动化测试实践>.   (一). 携程的无线App自动化     <携程无线A ...

  3. 实现基于Task的异步模式

    返回该系列目录<基于Task的异步模式--全面介绍> 生成方法 编译器生成 在.NET Framework 4.5中,C#编译器实现了TAP.任何标有async关键字的方法都是异步方法,编 ...

  4. http 各个状态码及对应的java 编程

    http的状态? 200 301 302 400 404 500 501 等等 如何编码? 其实这个是web服务器的范畴.服务器处理各个请求的时候,如果正常, 自然就是200 http://www.c ...

  5. MVVM架构~knockoutjs系列之级联select

    返回目录 对于下拉列表框的绑定在之前的knockoutjs文章中已经介绍过,今天主要说一下级联的select,事实上,在knockoutjs里,是以数据绑定为中心的,而数据是以面向对象为前提的,而对于 ...

  6. fir.im Weekly - Stanford 的 Swift 课程来了

    上周提过,Swift 的 Github 主页上已经有了 >>「Port to Android」,这周重点推荐一下 Stanford 的 Swift 课程. Developing iOS 9 ...

  7. 请求一个action,将图片的二进制字节字符串在视图页面以图片形式输出

    有些时候需要将二进制图片字节在发送浏览器以图片形式显示: 下面是一些示例代码: 控制器: /// <summary> /// 将图片的二进制字节字符串在视图页面以图片形式输出 /// &l ...

  8. Python的datetime

    Python的datetime 总会用到日期格式化和字符串转成日期,贴点代码以供参考,其实API真的是很全的,可是又不知道具体的method... datetime.datetime.strftime ...

  9. 使用XSD校验Mybatis的SqlMapper配置文件(1)

    这篇文章以前面对SqlSessionFactoryBean的重构为基础,先简单回顾一下做了哪些操作: 新建SqlSessionFactoryBean,初始代码和mybatis-spring相同: 重构 ...

  10. Ubuntu16配置静态IP

    一.静态IP地址配置 sudo vi /etc/network/interfaces 然后按照如下格式修改: 注意这里的网卡名字是ens33 auto lo iface lo inet loopbac ...