[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 sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
Example 1:
Input: [1,2,3] 1
/ \
2 3 Output: 6
Example 2:
Input: [-10,9,20,null,null,15,7] -10
/ \
9 20
/ \
15 7 Output: 42
这道求二叉树的最大路径和是一道蛮有难度的题,难就难在起始位置和结束位置可以为任意位置,博主当然是又不会了,于是上网看看大神们的解法,像这种类似树的遍历的题,一般来说都需要用 DFS 来求解,先来看一个简单的例子:
/ \ / \
由于这是一个很简单的例子,很容易就能找到最长路径为 7->11->4->13,那么怎么用递归来找出正确的路径和呢?根据以往的经验,树的递归解法一般都是递归到叶节点,然后开始边处理边回溯到根节点。这里就假设此时已经递归到结点7了,其没有左右子节点,如果以结点7为根结点的子树最大路径和就是7。然后回溯到结点 11,如果以结点 11 为根结点的子树,最大路径和为 7+11+2=20。但是当回溯到结点4的时候,对于结点 11 来说,就不能同时取两条路径了,只能取左路径,或者是右路径,所以当根结点是4的时候,那么结点 11 只能取其左子结点7,因为7大于2。所以,对于每个结点来说,要知道经过其左子结点的 path 之和大还是经过右子节点的 path 之和大。递归函数返回值就可以定义为以当前结点为根结点,到叶节点的最大路径之和,然后全局路径最大值放在参数中,用结果 res 来表示。
在递归函数中,如果当前结点不存在,直接返回0。否则就分别对其左右子节点调用递归函数,由于路径和有可能为负数,这里当然不希望加上负的路径和,所以和0相比,取较大的那个,就是要么不加,加就要加正数。然后来更新全局最大值结果 res,就是以左子结点为终点的最大 path 之和加上以右子结点为终点的最大 path 之和,还要加上当前结点值,这样就组成了一个条完整的路径。而返回值是取 left 和 right 中的较大值加上当前结点值,因为返回值的定义是以当前结点为终点的 path 之和,所以只能取 left 和 right 中较大的那个值,而不是两个都要,参见代码如下:
class Solution {
public:
int maxPathSum(TreeNode* root) {
int res = INT_MIN;
helper(root, res);
return res;
}
int helper(TreeNode* node, int& res) {
if (!node) return ;
int left = max(helper(node->left, res), );
int right = max(helper(node->right, res), );
res = max(res, left + right + node->val);
return max(left, right) + node->val;
}
};
讨论:这道题有一个很好的 Follow up,就是返回这个最大路径,那么就复杂很多,因为这样递归函数就不能返回路径和了,而是返回该路径上所有的结点组成的数组,递归的参数还要保留最大路径之和,同时还需要最大路径结点的数组,然后对左右子节点调用递归函数后得到的是数组,要统计出数组之和,并且跟0比较,如果小于0,和清零,数组清空。然后就是更新最大路径之和跟数组啦,还要拼出来返回值数组,代码长了很多,有兴趣的童鞋可以在评论区贴上你的代码~
Github 同步地址:
https://github.com/grandyang/leetcode/issues/124
类似题目:
参考资料:
https://leetcode.com/problems/binary-tree-maximum-path-sum/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 124. 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. ...
- leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- [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 ...
- 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 ...
- leetcode 124. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- leetcode 124. Binary Tree Maximum Path Sum ----- java
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- leetcode@ [124] Binary Tree Maximum Path Sum (DFS)
https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...
- [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. ...
随机推荐
- Preface_英语
这是一本游戏指南.没错,你没有 看错,这就是一本游戏指南.当然,这 本指南针对的只是名为"英文"的游戏. 把英文和电子游戏比较一下,我们 会发现,这两者有惊人的相似之处. 第一,它 ...
- 机器学习(十一)-------- 异常检测(Anomaly Detection)
异常检测(Anomaly Detection) 给定数据集
- 【UOJ#60】【UR #5】怎样提高智商
[UOJ#60][UR #5]怎样提高智商 题面 UOJ 题解 首先猜猜答案是\(4*3^{n-1}\).即前面的选啥都行,后面的搞搞就行了. 而打表(看题解),可以知道答案就是这个,并且每个问题都是 ...
- Oracle 原生驱动带来的精度问题的分析与解决
问题 Oracle 官方提供了 dotnet core 驱动,但我们在使用中遇到了精度问题. 复现 以下代码运行数学运算 1/3,无论是 OracleCommand.ExecuteScalar() 还 ...
- C#获取文件夹下所有的文件名称
例如想获取后缀名为.txt的文件 //第一种方法 var files = Directory.GetFiles(path, "*.txt"); foreach (var file ...
- 关于 L3 缓存行 cacheLIne 的研究!还是对程序有举足轻重的作用!
https://www.cnblogs.com/PurpleTide/archive/2010/11/25/1887506.html CLR via C# 读书笔记 2-3 Cache Lines a ...
- WEBAPI 设置上传文件大小
参考资料:https://stackoverflow.com/questions/33399267/cors-error-when-uploading-larger-files https:// ...
- 从VisualStudio资源文件看.NET资源处理
c# 工程里面,经常会添加资源文件. 作用: 一处文本多个地方的UI使用,最好把文本抽成资源,多处调用使用一处资源. 多语言版本支持,一份代码支持多国语言.配置多国语言的资源文件,调用处引用资源. 例 ...
- 4-consul HTTP API及实践
其他参考:https://www.cnblogs.com/duanxz/p/9660766.html 原文:https://www.douban.com/note/629645759/ 注意:使用AP ...
- ORM:对象关系映射
一.简单操作 定义:面向对象和关系型数据库的一种映射,通过操作对象的方式操作数据 对应关系: 类对应数据表 对象对应数据行(记录) 属性对应字段 导入:from app01 import models ...