题目:

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. 跨站请求伪造CSRF

    CSRF是Cross Site Request Forgery的缩写,乍一看和XSS差不多的样子,但是其原理正好相反,XSS是利用合法用户获取其信息,而CSRF是伪造成合法用户发起请求. 在XSS危害 ...

  2. 这里有个坑---[NotMapped]不要忘了加

    这里有个坑,我们定义实体的时候如果使用了在数据库没有的字段记得加[NotMapped]---------每一个遇到的坑总结后都是一比财富. 在项目中我们一般会使用EF等ORM框架来为我们服务,在ORM ...

  3. KnockoutJS 3.X API 第四章 表单绑定(11) options绑定

    目的 options绑定主要用于下拉列表中(即<select>元素)或多选列表(例如,<select size='6'>).此绑定不能与除<select>元素之外的 ...

  4. ThinkPHP5 隐藏接口里面的index.php

    隐藏index.php 官方介绍是这样的:http://www.kancloud.cn/thinkphp/thinkphp5_quickstart/145250 可以去掉URL地址里面的入口文件ind ...

  5. 体验Visual Studio 2015 Windows Forms应用程序开发与维护

    昨天到半夜还没有等到Visual Studio 2015的下载地址,实在熬不住就先休息了.北美地区的时区比北京时间要晚一些,今天早上到公司就看到Visual Studio 2015的下载地址,迅速的将 ...

  6. 面向对象tab栏例子分析

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. javascript类型系统——日期Date对象

    × 目录 [1]静态方法 [2]构造函数 [3]实例方法 前面的话 Date对象是javascript语言中内置的数据类型,用于提供日期和时间的操作接口.Date对象是在早期java中的java.ut ...

  8. 深入理解CSS计数器

    × 目录 [1]创建计数器 [2]使用计数器 [3]DEMO 前面的话 我们对计数器已经不陌生了,有序列表中的列表项标志就是计数器. 创建计数器 创建计数器的基础包括两个方面,一是能重置计数器的起点, ...

  9. [c++] Pieces of knowledge

    再系统地学一遍c++,查缺补漏. wchar_t L'a' stored in wchar_t. size of types cout << numeric_limits<doubl ...

  10. ISTool5.3.1汉化版使用教程

    ISTool是帮助你创建由Jordan Russell制作的Inno Setup编译器脚本的工具.Inno是一个出色的编译器,即使对于某些专业的安装制作程序,它唯一的缺点就是它的脚本必须手动编写. 这 ...