[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的。递归式是应该是这三者中选出最大者。这题是看完yucoding的博客才算可能理解,这里只是用中文讲解该博客中的分析过程。举例子:

对树中的任一节点,当有一条路径经过它时(不一定为最大),有两种情况:
1)“顶节点”为当前节点时,如当前节点为2时,路径为6->4->2->5->-3;
2)“顶节点”为当前节点的父节点1,当前节点为2时,路径为-3->5->2->1->-3->6
对某个节点a,最大路径为:
i) max_top(a)为第一种情况下的最大路径和;
ii) max_single(a)为第二种情况下的最大路径和;
则,max_top(a)=Max{max_single(a), max_single(a->left)+max_single(a->right)+a->val, a->val};
max_single(a)=Max{max_single(a->left)+a->val, max_single(a->right)+a->val, a->val};
最每个节点a,res=max(res, max_top(a))。
其实,个人这样理解的,以当前点为“顶结点”,则,需从只有一条子树的和、两条子树加顶点的和、该顶点的值三种中选出最大值作为所求值;若以当前点的父结点为顶结点,说明这条路径必须经过该父结点,所以,求经过当前结点的路径,只能是从叶结点到当前结点(再到父结点),即只有一条而不能是两条之和,若是再求两条之后,则后续就不能通过该父结点了。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
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 ;
int left = maxPathSumDFS(root->left, res);
int right = maxPathSumDFS(root->right, res);
int top = root->val + (left > ? left : ) + (right > ? right : ); //第一种
res = max(res, top);
return max(left, right) > ? max(left, right) + root->val : root->val; //第二种
}
};
//代码来源Grandyang
[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. ...
- [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 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 ...
- 二叉树系列 - 二叉树里的最长路径 例 [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 @ Python
原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...
- 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# WebBrowser开发参考资料--杂七杂八
c# WebBrowser开发参考资料 http://hi.baidu.com/motiansen/blog/item/9e99a518233ca3b24aedbca9.html=========== ...
- mysql日志管理#慢日志详解
MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志中 long_q ...
- Python进阶-函数默认参数
Python进阶-函数默认参数 写在前面 如非特别说明,下文均基于Python3 一.默认参数 python为了简化函数的调用,提供了默认参数机制: def pow(x, n = 2): r = 1 ...
- Logistic Regression学习笔记
1.李航<统计学习方法>: 2.https://blog.csdn.net/laobai1015/article/details/78113214 3.http://www.cnblogs ...
- 原lnmp环境服务器升级为mysql+nginx+php单个docker容器构建的lnmp环境
时间:2018年2月 一.项目背景 我单位现web服务架构为lnmp环境,服务器软件.硬件升级部署难:同时开源软件日新月异,考虑到技术升级,领导决定服务器架构整体升级为容器架构,维护性.移植性强. 二 ...
- Apache Tomcat 整合
Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.Apache+Tomcat整合是什么: 1.Apache默认访问端口是80,Tomcat默认访问端口是8080 ...
- JENKINS系统的安装部署
JENKINS 安装使用文档 简介 Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何类型的构建或持续集成,集成Jenkins可 ...
- 关于cookie的一些学习笔记
0x00 发现自己对一些原理性的东西实在是太不了解 最近看了<cookie之困>记一下笔记 0x01 因为http是无状态的 所以需要cookie和session来保持http的会话状态和 ...
- Linq中dbSet 的查询
1.Find:按照关键字的ID号来查询(速度快) 如: ADShiTi aDShiTi = db.ADShiTis.Find(id); 2.FirstOrDefault:根据部分条件查询,显示最前的一 ...
- Mac OS下搭建Hadoop + Spark集群
首先注意版本兼容问题!!!本文采用的是Scala 2.11.8 + Hadoop 2.7.5 + Spark 2.2.0 请在下载Spark时务必看清对应的Scala和Hadoop版本! 一.配置JD ...