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/ 给一棵二叉树,路径可以从任一点起,到任一点结束,但是可以连成一个路径的.求 ...
随机推荐
- 温故而知新 $ jquery选择器居然隐藏第二个参数,更进一步限制选择的区域
$ 选择器的第二个参数 $("[name=" + name + "]", layero); layero 其实也是一个dom对象,譬如一个表单,一个table. ...
- java程序猿常用Linux命令
1.查找文件 find / -name filename.txt 根据名称查找/目录下的filename.txt文件. find . -name "*.xml" 递归查找所有的xm ...
- unity, stateMachine, change state name
如图,假设wingsLayer下有个state,叫“New State“,现在想给它改名,则一定要保证当前wingsLayer为选中状态,然后才能在Inspector中可以为其改名.否则若当前选中的不 ...
- php调试函数
void debug_print_backtrace ([ int $options = 0 [, int $limit = 0 ]] ) array debug_backtrace ([ int $ ...
- JSON传参
通过javascript将数据组织成json格式,然后传到java后台. 注意:前台json数组传参到后台时候需要将对象(json或json数组)转换成字符串(字符串数组). Simple: 1.前台 ...
- 跑酷游戏的一些bug总结(滥用FixedUpdate的坑)
最近把1年前的跑酷游戏demo拿出来重做了一遍,解决了2个之前的遗留bug. 虽然罪魁祸首都是FixedUpdate,但细节又不太一样.这里记录一下 1.点击空格键,角色会跳跃.而有时会跳的比之前高很 ...
- android.animation(2) - ValueAnimator的 Interpolator 和 Evaluator
一.插值器 插值器,也叫加速器:有关插值器的知识,我在<Animation动画详解(二)——Interpolator插值器>中专门讲过,大家可以先看看这篇文章中各个加速器的效果.这里再讲一 ...
- 632. Binary Tree Maximum Node【Naive】
Find the maximum node in a binary tree, return the node. Example Given a binary tree: 1 / \ -5 2 / \ ...
- codeblocks 配置OpenGL
一.选择编译器环境 这里选择codeblocks,带MinGW的版本. 二.下载glut工具包 网址:http://pan.baidu.com/s/1eQriTQM 三.配置glut 解压缩下载的gl ...
- docker + ubuntun 安装show doc
基本安装步骤 Ubuntu Docker 安装 Docker 支持以下的 Ubuntu 版本: Ubuntu Precise 12.04 (LTS) Ubuntu Trusty 14.04 (LTS) ...