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.

题目意思很简单,就是给定一棵二叉树,求最大路径和。path 可以从任意 node 开始,到任意 node 结束。

这道题在 LeetCode 上的通过率只有 20% 多一点,并被标记为 Hard ,但实际上这是一道相当好的问题,在北美的 CS 求职面试中也是一道高频题,下文我将尝试用最容易理解的方式去分析这道题目。

我的思路

1. 我们直接从题目的问题来分析。给定一棵二叉树,我们怎么来求其最大路径和呢?稍做思考,不难得到以下结论:

当前二叉树的双路

最大路径和 = max(左子树的双路最大路径和, 右子树的双路最大路径和, 当前二叉树在包含根结点情况下的双路最大路径和);

2. 很显然,要解决上面的问题,关键在于怎么求二叉树在包含根结点情况下的双路最大路径和,而要求二叉树在包含根结点情况下的双路最大路径和,首先得先求二叉树的单路最大路径和,请看以下分析:

当前二叉树的单路

最大路径和 = max(左子树的单路最大路径和 + 根结点值, 右子树的单路最大路径和 + 根结点值, 根结点值);

当前二叉树在包含根结点情况下的双路

最大路径和 = 左子树的单路最大路径和 + 根结点值 + 右子树的单路最大路径和;

3. 通过 1 和 2,我们很容易发现,该题可以采用分治法来解决,实际上,与二叉树有关的绝大多数问题,我们都可以采用分治的思想。通常,分治法避免使用全局变量,因此我将会封装一个 returnType 类型,来作为返回值,如下:

struct returnType
{
returnType(const int maxSinglePathSum, const int maxDoublePathSum)
: maxSinglePathSum_(maxBranch), maxDoublePathSum_(maxSum)
{ } int maxSinglePathSum_;
int maxDoublePathSum_;
};

4. 可以 AC 的完整源代码如下:

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ struct returnType
{
returnType(const int maxSinglePathSum, const int maxDoublePathSum)
: maxSinglePathSum_(maxSinglePathSum), maxDoublePathSum_(maxDoublePathSum)
{ } int maxSinglePathSum_;
int maxDoublePathSum_;
}; class Solution
{
public:
    int maxPathSum(TreeNode *root)
{
return maxPathRec(root).maxDoublePathSum_;
} returnType maxPathRec(TreeNode *root)
{
if (root == NULL)
{
return returnType(0, numeric_limits<int>::min());
} // Devide
returnType left = maxPathRec(root->left);
returnType right = maxPathRec(root->right); // Conquer
int subMaxSinglePathSum = max(left.maxSinglePathSum_, right.maxSinglePathSum_);
int maxSinglePathSum = root->val;
maxSinglePathSum = max(subMaxSinglePathSum + maxSinglePathSum, maxSinglePathSum); int subMaxDoublePathSum = max(left.maxDoublePathSum_, right.maxDoublePathSum_);
int maxDoublePathSum = max(maxSinglePathSum, left.maxSinglePathSum_ + root->val + right.maxSinglePathSum_);
maxDoublePathSum = max(subMaxDoublePathSum, maxDoublePathSum); return returnType(maxSinglePathSum, maxDoublePathSum);
}
};

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

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

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

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

  6. leetcode–Binary Tree Maximum Path Sum

    1.题目说明 Given a binary tree, find the maximum path sum.   The path may start and end at any node in t ...

  7. C++ leetcode Binary Tree Maximum Path Sum

    偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点. 题目: Given a binary tree, find the maximum path su ...

  8. [leetcode]Binary Tree Maximum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...

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

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

随机推荐

  1. LocalDateTime json格式化

    参考https://www.cnblogs.com/xiaozhang9/p/jackson.html?utm_source=itdadao&utm_medium=referral <d ...

  2. 使用UUID方法生成全球唯一标识

    需要生成唯一字符串,如生成应用标识等,可以直接用java.util.UUID类实现. UUID(Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字, ...

  3. 分享至微信、QQ、微博、复制链接

    var share = { "tit": '您的朋友分享了文章', "desc": '分享来自百度文库,包含...', "pic": 'ht ...

  4. ASP.NET中修改从数据库获取的datatable中的值

    有些时候,我们从数据库表中获取一个实体的对象,但有些内容并不是最终显示的内容,格式也都是不一样.经过一番尝试,发现datatable中的数值如果跟想要改变的类型不一致,就无法更改,只有添加新列,然后把 ...

  5. day1:vcp考试

    Q1. An administrator wants to provide users restricted access. The users should only be able to perf ...

  6. mysql中float、double、decimal三种类型,以及数值产生误差的原因

    单精度浮点数用4字节(32bit)表示浮点数,采用IEEE754标准的计算机浮点数,在内部是用二进制表示的,如:7.22用32位二进制是表示不下的,所以就导致不精确了,存取会出现误差. mysql中f ...

  7. MSVCR90D.dll

    http://stackoverflow.com/questions/218747/msvcr90d-dll-not-found-in-debug-mode-with-visual-c-2008 I ...

  8. hreeJS加载Obj资源后如何实现内存释放?

    问题: 我利用ThreeJS做了一个在同一个场景下动态加载Obj的页面,具体功能是:点击按钮A:加载A模型,点击按钮B:加载B模型...现在的问题是,前面已经加载过的模型,无法实现释放,内存一直在累加 ...

  9. MySQL主从复制备份

    前言 数据库实时备份的需求很常见,MySQL本身提供了 Replication 机制,摘译官方介绍如下: MySQL Replication 可以将一个主数据库中的数据同步到一个或多个从数据库中.并且 ...

  10. linux-redhat-git源码安装

    1.查看是否已安装git,如果存在自带的git,则卸载 查看git版本 $ git --version 删除自带git $ yum remove git 2.安装依赖包 $ yum -y instal ...