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

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);
    }
};
时间: 2024-10-07 20:22:09

[LeetCode] Binary Tree Maximum Path Sum(最大路径和)的相关文章

LeetCode: Binary Tree Maximum Path Sum [124]

[题目] 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. [题意] 给定一棵二叉树,找出其中路径和最大的路径,然会返回最大路径和. 本题中的路径不是从根节点到叶子节点这样的传统的路径,而是指的二叉树中任意两个节点之间的联通路径.

[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. For example: Given the below binary tree, 1 / 2 3 Return 6. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNo

[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. For example: Given the below binary tree, 1 / 2 3 Return 6. [分析]    需要考虑以上两种情况: 1 左子树或者右子树中存有最大路径和 不能和根节点形成一个路径 2 左子树 右子树 和根节点形成最大路径 [代码] /******

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven 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 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

[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. For example:Given the below binary tree, 1 / 2 3 Return6. 思路:题目中说明起始节点可以是任意节点,所以,最大的路径和不一样要经过root,可以是左子树中某一条,或者是右子树中某一条,当然也可能是经过树的根节点root的.递归式是应该是这三者中

[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. For example:Given the below binary tree, 1 / 2 3 Return 6. 这道求二叉树的最大路径和是一道蛮有难度的题,难就难在起始位置和结束位置可以为任意位置,我当然是又不会了,于是上网看看大神们的解法,看了很多人的都没太看明白,最后发现了网友Yu's C

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. For example: Given the below binary tree, 1 / 2 3 Return 6. 原题链接:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题目:给定一二叉树,求出最大路径和. 分析:

leetcode&mdash;&mdash;Binary Tree Maximum Path Sum

    这题很难,主要是我没理解题目的意思,后来看着给的测试参考,和网上找的答案,终于理解了. 例如: 给的 正确的路线应该是   也就是说,路径是一条的,不是有分叉的,之所以算出55是因为把>0的数都加上去了,这样路径就分叉了,就不是一条路径了,所以用dfs做时,返回值应该是左子树或者右子树中>0且比较大的那个子树的路径的值加上当前根节点的值. 我的是参考网上答案的代码: // 用dfs,因为路径是任意的,所以根节点也是任意的,如果左子树的值>0,即对sum有贡献,则sum加其值,右子

leetcode -day9 Candy &amp; Gas Station &amp; Binary Tree Maximum Path Sum

1.  Candy There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get m