题目:

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. PHP面向对象04_串行化

    oop04复习 2014-9-3 10:48:45 要点: --1.克隆对象 --2.__toString( ) --3. __call( ) --4.自动加载类 --5.对象串行化 1.克隆对象以及 ...

  2. lua二进制操作函数

    由于 Lua 脚本语言本身不支持对数字的二进制操作(例如 与,或,非 等操作),MUSHclient 为此提供了一套专门用于二进制操作的函数,它们都定义在一个“bit”表中,使用时只要requre “ ...

  3. js const

    js const const 声明创建一个只读的常量.这不意味着常量指向的值不可变,而是变量标识符的值只能赋值一次. const state = { notes: [], activeNote: {} ...

  4. 基于asp.net+MINIUI的项目----在线学习系统

    1 数据库列的自动计算: 描述:一张选课表,其中有学习的开始时间和结束时间,一个列用来计算学习的总时间(小时) 解决:选择该列 属性:计算列规范:公式:(datediff(hour,[StartTim ...

  5. Objective-C中@property的所有属性详解

    1,assign : 简单赋值,不更改索引计数 假设你用malloc分配了一块内存,并且把它的地址赋值给了指针a,后来你希望指针b也共享这块内存,于是你又把a赋值给(assign)了b.此时a 和b指 ...

  6. Android线程机制——AsyncTask

    对于Android为什么要使用多线程,因为从Android4.0之后,谷歌规定了网络操作不允许放在主线程中执行,由此就有了多线程的机制,有个JAVA学习经验的朋友一定知道多线程指的是什么,简单来讲就是 ...

  7. MongoDB的学习--索引类型和属性

    索引类型 MongDB的索引分为以下几种类型:单键索引.复合索引.多键索引.地理空间索引.全文本索引和哈希索引 单键索引(Single Field Indexes) 在一个键上创建的索引就是单键索引, ...

  8. PHP的学习--解析URL

    PHP中有两个方法可以用来解析URL,分别是parse_url和parse_str. parse_url 解析 URL,返回其组成部分 mixed parse_url ( string $url [, ...

  9. JavaScript作用域原理(一)——作用域链

    一.作用域的描述 JavaScript权威指南中对作用域有一句很精辟的描述:“JavaScript中的函数运行在它们被定义的作用域里,而不是它们被执行的作用域里.” 在JavaScript中,作用域的 ...

  10. Hadoop阅读笔记(五)——重返Hadoop目录结构

    常言道:男人是视觉动物.我觉得不完全对,我的理解是范围再扩大点,不管男人女人都是视觉动物.某些场合(比如面试.初次见面等),别人没有那么多的闲暇时间听你诉说过往以塑立一个关于你的完整模型.所以,第一眼 ...