Leetcode-Bianry 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
.
Analysis:
The previous solution is too complex. We actually only need to consider the max path from some child node to current root node, and the max path from one child node to another.
Two important points:
1. For null node, the singlePath is 0 but the endPath is Integer.MIN_VALUE;
2. We need consider about the case in which node value is negative.
Solution:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ class Result{
int singlePath;
int endPath; public Result(){
singlePath = 0;
endPath = Integer.MIN_VALUE;
} public Result(int s, int e){
singlePath = s;
endPath = e;
}
} public class Solution {
public int maxPathSum(TreeNode root) {
Result res = maxPathSumRecur(root);
return res.endPath; } public Result maxPathSumRecur(TreeNode cur){
if (cur==null){
Result res = new Result();
return res;
} Result left = maxPathSumRecur(cur.left);
Result right = maxPathSumRecur(cur.right);
Result res = new Result(); res.singlePath = Math.max(left.singlePath, right.singlePath);
res.singlePath = Math.max(res.singlePath,0);
res.singlePath += cur.val; res.endPath = Math.max(left.endPath, right.endPath);
int temp = cur.val;
if (left.singlePath>0) temp+=left.singlePath;
if (right.singlePath>0) temp+=right.singlePath;
res.endPath = Math.max(res.endPath, temp); return res;
} }
Previous Solution:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ //NOTE: Need to consider about negtive number, or ask interviewer about this issue!
//NOTE2: at every node, we need consider about three cases.
//1. the path start from some node in the lower level and end at the current node, called singlePath.
//2. the path from some child node in the left and end at some child node at right, called combinePath.
//3. the path that does not contain the current node, called lowPath.
//curNode:
//singlePath = max(left.singlePath, right.singlePath, curNode.val);
//combinePath = curNode.val+left.singlePath+right.singlePath;
//lowPath = max(left.combinePath, left.singlePath, left.lowPath, right.ALLPATH);
//Return:
//max(root.singlePath, root.combinePath, root.lowPath);
class PathInfo{
public int singlePath;
public int combinePath;
public int lowPath;
public int singleNodePath; public PathInfo(){
singlePath = 0;
combinePath = 0;
lowPath = 0;
}
} public class Solution {
public int maxPathSum(TreeNode root) {
PathInfo rootInfo = new PathInfo();
rootInfo = maxPathSumRecur(root); int max = rootInfo.singlePath;
if (rootInfo.combinePath>max)
max = rootInfo.combinePath;
if (rootInfo.lowPath>max)
max = rootInfo.lowPath; return max;
} public PathInfo maxPathSumRecur(TreeNode curNode){
//If current node is a leaf node
if (curNode.left==null&&curNode.right==null){
PathInfo path = new PathInfo();
path.singlePath = curNode.val;
path.combinePath = curNode.val;
path.lowPath = curNode.val;
return path;
} //If not, then get the PathInfo of its child nodes.
PathInfo left = null;
PathInfo right = null;
PathInfo cur = new PathInfo();
if (curNode.left!=null)
left = maxPathSumRecur(curNode.left);
if (curNode.right!=null)
right = maxPathSumRecur(curNode.right); //Now calculate the PathInfo of current node.
if (right==null)
cur.singlePath = curNode.val+left.singlePath;
else if (left==null)
cur.singlePath = curNode.val+right.singlePath;
else {
if (left.singlePath>right.singlePath)
cur.singlePath = curNode.val+left.singlePath;
else
cur.singlePath = curNode.val+right.singlePath;
}
if (cur.singlePath<curNode.val)
cur.singlePath=curNode.val; if (right==null)
cur.combinePath = curNode.val+left.singlePath;
else if (left==null)
cur.combinePath = curNode.val+right.singlePath;
else
cur.combinePath = curNode.val+left.singlePath+right.singlePath; int max = Integer.MIN_VALUE;
if (right==null){
max = left.lowPath;
if (left.combinePath>max)
max = left.combinePath;
} else if (left==null){
max = right.lowPath;
if (right.combinePath>max)
max = right.combinePath;
} else {
max = left.lowPath;
if (left.combinePath>max)
max = left.combinePath;
if (right.lowPath>max)
max = right.lowPath;
if (right.combinePath>max)
max = right.combinePath;
}
if (max<cur.singlePath)
max=cur.singlePath; cur.lowPath = max; return cur;
}
}
递归求解:对于当前node,计算三种情况的max path sum.
Leetcode-Bianry 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: 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] 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
1.题目说明 Given a binary tree, find the maximum path sum. The path may start and end at any node in t ...
- C++ leetcode Binary Tree Maximum Path Sum
偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点. 题目: Given a binary tree, find the maximum path su ...
- [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求二叉树最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- LeetCode OJ-- Binary Tree Maximum Path Sum ***
https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 给一棵二叉树,路径可以从任一点起,到任一点结束,但是可以连成一个路径的.求 ...
随机推荐
- Python 爬虫实例(6)—— 爬取蚂蚁免费代理
数据库表sql语句: CREATE TABLE `free_ip` ( `free_ip_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `ip` ...
- 连接到 Linux 服务器时首先要运行的 5 个命令
作为一个系统管理员/SRE 工作 5 年后,我知道当我连接到一台 Linux 服务器时我首先应该做什么.这里有一系列关于服务器你必须了解的信息,以便你可以(在大部分时间里)更好的调试该服务器. 连上 ...
- ecshop报错”Deprecated: Assigning the return value of…”解决办法
ECSHOP生成站点地图提示”Deprecated: Assigning the return value of new by reference is deprecated in…”. 我的问题在批 ...
- Genral log(普通日志)与 Slow log(慢速日式)
General log: Geleral log记录了服务器接收到的每一个查询或是命令,无论这些查询或是命令是否正确甚至是否包含语法错误,general log 都会将其记录下来 ,记录的格式为 {T ...
- 第二节 JVM优化应用以及知识总结
在JVM中.假设98%的时间是用于GC且可用的HeapSize不足2%时将会抛出OOM异常:HeapSize最大不要超过可用物理内存的80%,一般-Xms –Xmx设置为同样,-Xmn设置为1/4的- ...
- 关于TimeSpan
一秒是1000万个tick TimeSpan ts = * ); Console.WriteLine(ts); Console.Read(); //print 00:00:01 并且在TimeSpan ...
- print()函数的end 参数
print()函数含end参数时:结束的时候已什么结尾,后面的参数可以是任何形式 [print() 默认以'\n' 结尾] 输出结果: print()函数不含end参数时: 输出结果:
- sass初学入门笔记(一)
我本身是个新手,一边学sass一边记下的笔记,可能有点罗嗦,但是复习起来的话还是比较全面直观的.当然,最重要的还是去实践,实践得真理 其它 CSS 预处理器语言: CSS 预处理器技术已经非常的成熟, ...
- 0062 Spring MVC的文件上传与下载--MultipartFile--ResponseEntity
文件上传功能在网页中见的太多了,比如上传照片作为头像.上传Excel文档导入数据等 先写个上传文件的html <!DOCTYPE html> <html> <head&g ...
- cocos2dx迷你地图
用CCRenderTexture就可以了,不知是否有更好的方法. if (!miniMap) { miniMap=CCSprite::create(); miniMap->setZOrder() ...