【Lintcode】094.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.
Example
Given the below binary tree:
1
/ \
2 3
return 6
.
题解:
Solution 1 () from here
class Solution {
public:
int maxPathSum(TreeNode *root) {
if (root == NULL) {
return ;
} int res = INT_MIN;
helper(root, res); return res;
} int helper(TreeNode *root, int &res) {
if (root == NULL) {
return ;
} int sum = root->val;
int leftMax = helper(root->left, res);
int rightMax = helper(root->right, res); if (leftMax > ) {
sum += leftMax;
}
if (rightMax > ) {
sum += rightMax;
} res = max(sum, res); return max(, max(leftMax, rightMax)) + root->val;
}
};
【Lintcode】094.Binary Tree Maximum Path Sum的更多相关文章
- 【LeetCode】124. 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】124. Binary Tree Maximum Path Sum 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【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 Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 26. 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 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: 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
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
随机推荐
- hdu 1203 I NEED A OFFER!(01背包)
题意:"至少一份offer的最大概率".即求拿不到offer的最小概率 (得到offer的最大概率 = 1 - 反例的最小概率). 状态转移方程:dp[j]= Min(dp[j], ...
- js高度line-height及宽度text-align:center居中插件
1.高度居中---在高度设为100%,无法直接使用line-height:100%;会不起效果 这是用于应对height:100%的插件 /** * 高度居中函数,用于应对高度设为100%时的居中 * ...
- 在tomcat中用jndi配置数据源启动java web程序
1.在web.xml中添加: <resource-ref> <res-ref-name>jdbc/MTSDB</res-ref-name> <re ...
- HDFS源码分析数据块复制监控线程ReplicationMonitor(一)
ReplicationMonitor是HDFS中关于数据块复制的监控线程,它的主要作用就是计算DataNode工作,并将复制请求超时的块重新加入到待调度队列.其定义及作为线程核心的run()方法如下: ...
- PowerBuilder -- Len(), LenA() 与 String, Blob
使用的是Powerbuilder12.5与Powerbuild9 不太一样 函数 String Blob Len() 返回字符数 返回字符数对应的字节数 LenA() 返回字节数 返回字符数对应的字节 ...
- ASP.NET RemoteAttribute远程验证更新问题
create时使用remote特性没有任何问题, update时,问题就大了,验证唯一性时需要排除自身,如果使用这个特性将无法正确的验证. 改进思路:将自动生成的标签属性改为手写,,并在url上面加上 ...
- hadoop 出现FATAL conf.Configuration: error parsing conf file,异常
FATAL conf.Configuration: error parsing conf file: com.sun.org.apache.xerces.internal.impl.io.Malfor ...
- Epplus使用技巧
废话不说,直接开始. 创建Excel工作表 private static ExcelWorksheet CreateSheet(ExcelPackage p, string sheetName) { ...
- alibaba canal安装笔记
canal是alibaba开源的基于mysql binlog解析工具,可利用它实现mysql增量订阅/消费,典型的应用场景如下图: 利用canal,可以将mysql的数据变化,通过解析binlog,投 ...
- myql 5.6 安装
环境: centos 6.5 192.168.9.28 4核4G 虚拟机 一. 安装编译源码所需要的工具和库 [root@localhost ~]# yum -y install gcc gcc- ...