题目:

Binary Tree Maximum Path Sum

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.

节点可能为负数,寻找一条最路径使得所经过节点和最大。路径可以开始和结束于任何节点但是不能走回头路。

这道题虽然看起来不同寻常,但是想一下,可以发现不外乎二叉树的遍历+简单的动态规划思想。

我们可以把问题拆分开:即便最后的最大路径没有经过根节点,它必然也有自己的“最高点”,因此我们只要针对所有结点,求出:如果路径把这个节点作为“最高点”,路径最长可达多少?记为max。然后在max中求出最大值MAX即为所求结果。和“求整数序列中的最大连续子序列”一样思路。

下面就是找各个“最高点”对应的max之间的关系了。

我们拿根节点为例,对于经过根节点的最大路径的计算方式为:

我们找出左子树中以左孩子为起点的最大路径长度a,和右子树中以右孩子为起点的最大路径长度b。然后这个点的 max = MAX(a+b+node.val, a+node.val, b+node.val, node.val)

因此我们定义一个函数来算上面的a或者b,它的参数是一个节点,它的返回值是最大路径长度,但是这个路径的起点必须是输入节点,而且路径必须在以起点为根节点的子树上。

那么函数func(node)的return值可以这样定义:return MAX(func(node.left)+node.val, func(node.right)+node.val, node.val)

终止条件是node == null,直接返回0。

接着我们发现上述计算max 和 求出MAX的过程完全可以放到func(node) 里去。

按照这个思路的代码,maxPathSumCore 就是上面 func(node)的实现:

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxPathSum(TreeNode *root) {
maxPathSumCore(root);
return MAX;
}
int maxPathSumCore(TreeNode *node) {
if(NULL == node) return ;
int a = maxPathSumCore(node -> left);
int b = maxPathSumCore(node -> right);
if((a+b+node->val) > MAX) MAX = (a+b+node->val);
if((a+node->val) > MAX) MAX = (a+node->val);
if((b+node->val) > MAX) MAX = (b+node->val);
if(node->val > MAX) MAX = node->val;
int maxViaThisNode = ((a + node->val) > node->val ? (a + node->val) : node->val);
return (maxViaThisNode > (b + node->val) ? maxViaThisNode : (b + node->val));
}
private:
int MAX= -;
};

时间复杂度 O(n),n为总节点数。

二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum的更多相关文章

  1. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  2. [Leetcode] Binary tree maximum path sum求二叉树最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  3. [LeetCode] Binary Tree Maximum Path Sum(最大路径和)

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  4. LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)

    题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...

  5. LeetCode 124. 二叉树中的最大路径和(Binary Tree Maximum Path Sum)

    题目描述 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: [1,2,3] 1 ...

  6. [LeetCode] 124. 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 ...

  7. 75.Binary Tree Maximum Path Sum(二叉树的最大路径和)

    Level:   Hard 题目描述: Given a non-empty binary tree, find the maximum path sum. For this problem, a pa ...

  8. LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)

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

  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. ElasticSearch 论坛搜索查询语句

    概述 研究论坛搜索如何综合时间和TF/IDF权重. 自定义权重计算的效率问题 数据结构 假设有一个论坛的搜索 字段包括: subject:标题 message:内容 dateline:发布时间 tag ...

  2. Thunder团队——事后诸葛亮会议

    小组名称:Thunder 项目名称:爱阅APP 小组成员:王航 李传康 代秋彤 邹双黛 苗威 宋雨 胡佑蓉 杨梓瑞 一.设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型 ...

  3. Python 服务器端表单验证插件

    Python格式验证库 Cerberus 作者 MrStranger 关注 2016.08.02 14:44 字数 2140 阅读 79评论 0喜欢 1 Cerberus是一个验证Python对象.M ...

  4. spring框架(1)— 依赖注入

    依赖注入 spring核心容器就是一个超级大工厂,所以的对象(数据源.hibernate SessionFactory等基础性资源)都会被当做spring核心容器的管理对象——spring把容器中的一 ...

  5. Jedis源码解析——Jedis和BinaryJedis

    1.基本信息 先来看看他们的类定义: public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands ...

  6. Jenkins系列-Jenkins邮件通知

    一.安装邮件插件 由于Jenkins自带的邮件功能比较鸡肋,因此这里推荐安装专门的邮件插件,不过下面也会顺带介绍如何配置Jenkins自带的邮件功能作用. 可以通过系统管理→管理插件→可选插件,选择E ...

  7. RT-thread内核之线程调度器

    一.前言 RT-Thread中提供的线程调度器是基于全抢占式优先级的调度,在系统中除了中断处理函数.调度器上锁部分的代码和禁止中断的代码是不可抢占的之外,系统的其他部分都是可以抢占的,包括线程调度器自 ...

  8. 2017中国大学生程序设计竞赛-哈尔滨站 A - Palindrome

    Palindrome Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Tota ...

  9. [洛谷P3833][SHOI2012]魔法树

    题目大意:给一棵树,路径加,子树求和 题解:树剖 卡点:无 C++ Code: #include <cstdio> #include <iostream> #define ma ...

  10. POJ3621:Sightseeing Cows——题解

    http://poj.org/problem?id=3621 全文翻译参自洛谷:https://www.luogu.org/problemnew/show/P2868 题目大意:一个有向图,每个点都有 ...