[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 3Return
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(最大路径和)的更多相关文章
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- [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 ...
- 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 ...
- [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] 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–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 ...
- C++ leetcode Binary Tree Maximum Path Sum
偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点. 题目: Given a binary tree, find the maximum path su ...
- [leetcode]Binary Tree Maximum Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...
- LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)
题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
随机推荐
- global statement
[global statement] 在线程里,默认所有变量都是本线程局部变量,要想访问全局变量,则要先用global声明. 如全局有变量totalCount,线程中有语句 totalCount += ...
- Django1.8:403错误:CSRF verification failed. Request aborted.
问题:Django 403错误:CSRF verification failed. Request aborted. 原因:需要加cookie验证 解决方法: 1.在view.py中增加 fr ...
- JPA调用函数
criteriaBuilder.function("udf_get_cc_userright", Integer.class, criteriaBuilder.literal(se ...
- tf.unstack\tf.unstack
tf.unstack 原型: unstack( value, num=None, axis=0, name='unstack' ) 官方解释:https://tensorflow.google.cn/ ...
- How Many Answers Are Wrong(带权并查集)
How Many Answers Are Wrong http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS ( ...
- Redis网络协议
Redis网络协议较为简单,易于阅读. 命令或数据已\r\n结尾,但除了状态回复,其他数据都是二进制安全的(包含长度) 头部如下: + 正确的状态信息,具体信息是当前行+后面的字符. - 一条错误信 ...
- [leetcode]636. Exclusive Time of Functions函数独占时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- 四种强制类型转换的总结(const_cast、static_cast、dynamic_cast、reinterpreter_cast)
四种强制类型转换的总结(const_cast.static_cast.dynamic_cast.reinterpreter_cast) 转载 2011年10月03日 23:59:05 标签: stru ...
- Spring框架的AOP技术之通知类型
1. 通知类型 * @Before -- 前置通知 * @AfterReturing -- 后置通知 * @Around -- 环绕通知(目标对象方法默认不执行的,需要手动执行) * @After - ...
- RNA-seq要做几次生物学重复?找出来的100%都是真正的应答基因
尹师妹:“哈师兄,做验证实验好辛苦,老板让我提高筛选差异基因的条件,尽量降低假阳性,我该怎么筛?” 小哈打开Evernote,给尹师妹看张表: “瞧见那个100%了吗?30 million mappe ...