[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 Coding Garden的博客,感觉讲解还比较清楚,像这种类似数的遍历的题,一般来说都需要用DFS来求解,我们先来看一个简单的例子:

4
   / \
 11 13
 / \
7  2

对于一条路径来说,可以分为两种情况,一是当顶节点是当前点,另一种是定节点是父节点。例如,对于节点11来说,

1. 当顶节点是当前节点,对于节点11,路径为 6->11->2

2. 当顶节点是父节点4时,对于节点11,路径为 7->11->4->13

对于DFS来说,其递归过程中必定会对某节点的子节点调用,那么其返回值应该适用于上面第二种情况,但是最终结果肯定是第一种情况,那么如果保存并更新最终结果呢,我们可以将其放在参数中传递。那么对于任意一个节点n来说,

DFS(n) = max(DFS(n->left) + n->val, DFS(n->right) + n->val, n->val);

top(n) = max(DFS(n), DFS(n->left) + DFS(n->right) + n->val, n->val);

res = max(res, top(n));

理解了上述三个公式,这道题就基本理解了,具体的再看看代码吧:

class Solution {
public:
    int maxPathSum(TreeNode *root) {
        int res = root->val;
        maxPathSumDFS(root, res);
        return res;
    }
    int maxPathSumDFS(TreeNode *root, int &res) {
        if (!root) return 0;
        int left = maxPathSumDFS(root->left, res);
        int right = maxPathSumDFS(root->right, res);
        int top = root->val + (left > 0 ? left : 0) + (right > 0 ? right : 0);
        res = max(res, top);
        return max(left, right) > 0 ? max(left, right) + root->val : root->val;
    }
};
时间: 2024-10-15 10:35:42

[LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和的相关文章

[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 [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 OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For exampl

[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% 多一点,并被标记

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/ 题目:给定一二叉树,求出最大路径和. 分析:

124. Binary Tree Maximum Path Sum *HARD* -- 二叉树中节点和最大的路径的节点和

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For exampl