[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 ...
随机推荐
- Python序列删除重复数据
## 对于列表来说,若不保持原有顺序,可以直接转换为set删除重复数据 nums = [1,2,32,2,2,4,3,2,3,42] nums = list(set(nums)) print(nums ...
- Angular简单总结
AngularJS AngularJS四大特征 MVC模式 双向绑定 依赖注入 模块化设计 AngularJS 表达式 AngularJS 表达式写在双大括号内{{expression }},可以包含 ...
- Go web表单验证
开发Web的一个原则就是,不能信任用户输入的任何信息,所以验证和过滤用户的输入信息就变得非常重要 必填字段 if len(r.Form["username"][0])==0{ // ...
- (数据科学学习手札21)sklearn.datasets常用功能详解
作为Python中经典的机器学习模块,sklearn围绕着机器学习提供了很多可直接调用的机器学习算法以及很多经典的数据集,本文就对sklearn中专门用来得到已有或自定义数据集的datasets模块进 ...
- 编译net core时nuget里全部报错,\obj\project.assets.json找不到
除了Nuget管理设置允许下载缺少的程序包 如果你自己设置的程序包源里有一个源访问不到,则可能出现下面错误,导致所有nuget无法还原. 而且在VS2017里不会出现这个 SDK,特别是你网上下载的其 ...
- C++11中std::forward的使用
std::forward argument: Returns an rvalue reference to arg if arg is not an lvalue reference; If arg ...
- 破解PHPStrom 10 and Pycharm
注册时选择 License server http://idea.lanyus.com/ 然后点击OK Pycharm -- License server http://idea.lanyus.com ...
- Linux 服务器 监控命令
1 top top类似于windows下面的资源管理器.不仅能够从服务器整体上展示服务器的大致情况,还可以看到具体进程 耗费资源的情况. 展示内存.cpu.交换分区等信息 如上图: 第一行主要描述系统 ...
- ES6 语法糖
重新认识ES6中的语法糖:https://segmentfault.com/a/1190000010159725
- 文本向量化及词袋模型 - NLP学习(3-1)
分词(Tokenization) - NLP学习(1) N-grams模型.停顿词(stopwords)和标准化处理 - NLP学习(2) 之前我们都了解了如何对文本进行处理:(1)如用NLTK文 ...