[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 sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
Example 1:
Input: [1,2,3]
1
/ \
2 3
Output: 6
Example 2:
Input: [-10,9,20,null,null,15,7] -10
/ \
9 20
/ \
15 7 Output: 42
思路
dfs:
2
/ \
1 -3
maxPath: 1 + 2 + 0
1. use maxPath to update result from all paths which is max path sum
2. divide a path into 3 parts:
a. left path: from root to left side down to 0 or more steps
b. right path: from root to right side down to 0 or more steps
c. cur root itself
3. maxPath = c + (a>0 ? a: 0 ) + (b > 0 ? b:0)
这是一道关于BST和recursion的经典题,需要掌握
最naive的想法是找到所有BST的path,返回max
发现, 任意一条path都有一个顶点(位置最高点)
我们用这个顶点来分解所有path
这样,以任意一个点为顶点的path就分解为
a. max_sum (左边path)
b. max_sum (右边path)
c. 顶点自己的value
进一步,
a + b + c 组成的人字形path的max path sum
2
/ \
1 -3
dfs的return value : 2(顶点自己的value必须加上,无论正负) + 1 (正数贡献自己) + 0 (-3为负数不做贡献就是及时止损了) = 3
跟 [leetcode]543. Diameter of Binary Tree二叉树直径 的思路基本一致。
代码
class Solution {
public int maxPathSum(TreeNode root) {
// corner case
if(root == null){return 0;}
/* 要么用个global variable放在class下,要么用长度为1的一维数组来存。
maxSum的value,可正可负,初始化为Integer.MIN_VALUE。
*/
int[] maxPath = new int[]{Integer.MIN_VALUE};
dfs(root, maxPath);
return maxPath[0];
}
// 递归求以root为顶点所有直上直下的path中,path sum最大的一条值。没有U-turn的
private int dfs(TreeNode root, int[]maxPath){
// left > 0 benefit sum, add to sum; left < 0 will worsen sum, default 0
int left = root.left != null ? Math.max(dfs(root.left, maxPath), 0) : 0;
int right = root.right != null ? Math.max(dfs(root.right, maxPath), 0) : 0;
int cur = root.val + left + right;
maxPath[0] = Math.max(maxPath[0], cur);
return root.val + Math.max(left, right);
}
}
[leetcode]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 ...
- 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 求二叉树的最大路径和
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 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 ----- java
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- 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 Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)
题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
随机推荐
- Oracle jdk 历史版本官方下载地址及下载方法
Oracle jdk 历史版本官方下载地址及下载方法 原文转载至:http://blog.csdn.net/chwshuang/article/details/54925950 平时要新装一个系统环境 ...
- CA单向认证和双向认证的区别?
1:单向认证,内容会被串改吗?
- 自己写的jQuery放大镜插件效果(一)(采用一张大图和一张小图片的思路)
这个思路的方法会带来一个小问题,就是当鼠标放到小图上去时,会开始加载大图片,网速不佳的时候,会出现加载慢的情况.但是放大的效果和你所给出的大图片的清晰度是一样的. 先看效果图: html代码: < ...
- SyntaxError: Non-ASCII character ‘\xe5′ in file
在写一个抓取网页的小脚本,运行起来总是出现这个错误 查了下Python的默认编码文件是用的ASCII码,你将文件存成了UTF-8也没用,解决办法很简单 只要在文件开头加入 # -*- coding: ...
- opencv查看源代码
这一节是一个插曲,有的人刚开始学opencv就看源代码,有的人直接拿着opencv的API用...... 学了一个多月opencv了,就是没找到源代码,想看的时候都是从网上找的,或者看网上说从哪个文件 ...
- Tornado之链接数据库
5 数据库 知识点 torndb安装 连接初始化 执行语句 execute execute_rowcount 查询语句 get query 5.1 数据库 与Django框架相比,Tornado没有自 ...
- servlet练习1
1. 编写一个Servlet,当用户请求该Servlet时,显示用户于几点几分从哪个IP(Internet Protocol)地址连线至服务器,以及发出的查询字符串(Query String).查询一 ...
- FlexPaper及二次开发
Flexpaper二次开发入门教程 http://ajava.org/course/web/?page=2
- Linux开机挂载windows共享文件夹
https://blog.csdn.net/zhaogang1993/article/details/79573271 (可行) 命令: mount -t cifs -o username=&quo ...
- 50. linux下查看tomcat日志
cd tomcat/logs/ tail -f catalina.out