[Leetcode][JAVA] 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
Return 6
.
对于树,我们可以找到其左右子树中终止于根节点的最大路径值,称为最大半路径值,如果都是正数,则可以把它们以及根节点值相加,如果其中有负数,则舍弃那一边的值(即置为零)。如此可以找到包含根节点的最大路径值。然后选择左右子树的最大半路径值中大的那个(负数则置为0)加上根节点的值作为新的最大半路径值传给父节点。
于是我们可以递归地考察每个子树,不断更新一个全局变量max。
基本情况为:子树为null,此时最大路径值应为0.
代码如下:
int max;
public int maxPathSum(TreeNode root) {
max = root==null?0:root.val;
findMax(root);
return max;
}
public int findMax(TreeNode root) {
if(root==null)
return 0;
int left = Math.max(findMax(root.left),0);
int right = Math.max(findMax(root.right),0);
max = Math.max(max, left+right+root.val);
return Math.max(left, right) + root.val;
}
[Leetcode][JAVA] 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 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 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- 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 ...
- Java for LeetCode 124 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 ----- 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]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 求二叉树的最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
随机推荐
- boost的线程池和内存池 智能指针
内存池为boost自带的 #include <boost/pool/pool.hpp> 或者另外一个开源的库: nedmalloc 一个高效率的库 线程池需要下载另外一个开源库 http: ...
- [原创] ubuntu下安装scrapy报错 error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
Ubuntu14.04在virtualenv下安装scrapy报错,Failed building wheel for cffi,lxml,cryptography 等. error: command ...
- mysql 主从复制 实践
异步主从复制 主从部署步骤: 备份还原 使用mysqldump或者xtrabackup 把主库现有基础数据还原到从库 授权 grant replication slave on *.* 给从库一个 ...
- 【转载】H264--2--语法及结构
名词解释 场和帧 : 视频的一场或一帧可用来产生一个编码图像.在电视中,为减少大面积闪烁现象,把一帧分成两个隔行的场. 片: 每个图象中,若干宏块被排列成片的形式.片分为 ...
- hello
#include <iostream> int main() { std::cout << "请输入两个数字:" << std::endl; , ...
- oracle 方向及资料
总结了一下大家的意见,也加了一些个人的看法,Oracle的学习路径,可供参考: 初级阶段: 可以从OCP教材开始,还有文档中的Administrator's Guide.Concepts.Perfor ...
- ElasticSearch 2 (10) - 在ElasticSearch之下(深入理解Shard和Lucene Index)
摘要 从底层介绍ElasticSearch Shard的内部原理,以及回答为什么使用ElasticSearch有必要了解Lucene的内部工作方式? 了解ElasticSearch API的代价 构建 ...
- ORACLE 分组之后容易被忽略的bug
COL_2 COL_321 3123 31 如上表数据 前台显示显示需要把COL_2的21和23转换成中文 ‘整机’ 最开始如下编写 SELECT t.col_3, CASE ...
- HQL
以下内容全部摘自韩顺平老师Hibernate笔记 * uniqueResult方法 如果我们检索一个对象,明确知道最多只有一个对象,则建议使用该方法: 具体用法如下: Student s=(Stude ...
- linux下查找某文件关键字
-e表示罗列出与关键字有关的行,“ABC”表示查找的关键字,/XXX/4.assoc.linear表示该路径下的文件 .assoc.linear