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. mysql for update语句

    我们都知道for update语句会锁住一张表,锁表的细节很多人却不太清楚,下面我们举例看下. 在表上我们有个索引,如下: 现在在我们通过索引store_id锁表: 我们再开一个客户端,还是锁住同一个 ...

  2. Process ProcessThread Thread

    Process ProcessThread: Process and ProcessThread objects have a ProcessorAffinity property of IntPtr ...

  3. oracle存储过程-获取错误信息

    dbms_output.put_line('code:' || sqlcode); dbms_output.put_line('errm:' || sqlerrm); dbms_output.put_ ...

  4. cdoj793-A Linear Algebra Problem

    http://acm.uestc.edu.cn/#/problem/show/793 A Linear Algebra Problem Time Limit: 3000/1000MS (Java/Ot ...

  5. 57. Insert Interval (Array; Sort)

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  6. popupMenu-----弹出菜单

    import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android ...

  7. java bulid path 和 WEB-INF/lib 下jar 包区别

    用Java Build Path导入包和把包复制到lib下是有区别的,它俩其实不会冲突,也没有什么关系的, Java Build Path是我们编译需要的包, 导入到lib下是程序运行时需要的包 ,  ...

  8. URL编码转换函数:escape()、encodeURI()、encodeURIComponent()

          函数出现时间:                      escape()                                javascript 1.0           ...

  9. Spring Boot之初始化项目

    最简单的从Spring官网下载 :地址 https://start.spring.io/ 下拉选择需要的Spring Boot版本 修改Group和Artifact  直接点击Generate Pro ...

  10. 项目UML设计

    团队信息 队名:火箭少男100 本次作业课上成员 短学号 名 本次作业博客链接 2507 俞辛(临时队长) https://www.cnblogs.com/multhree/p/9821080.htm ...