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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

  4. [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. ...

  5. 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 ...

  6. C++ leetcode Binary Tree Maximum Path Sum

    偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点. 题目: Given a binary tree, find the maximum path su ...

  7. [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. ...

  8. [leetcode]Binary Tree Maximum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...

  9. [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. ...

  10. LeetCode OJ-- Binary Tree Maximum Path Sum ***

    https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 给一棵二叉树,路径可以从任一点起,到任一点结束,但是可以连成一个路径的.求 ...

随机推荐

  1. Linux下从视频提取音频的方法

    Linux下可以利用mencoder将视频里的音频提取出来.方法如下: 1.首先安装mencoder.对于Ubuntu来说,软件仓库里就有mencoder,可直接输入如下命令安装 sudo apt-g ...

  2. Node.js验证码模块captchapng

    captchapng是一个基于pnglib模块开发,数字型验证码模块.内置字体.全JavaScript无其它依赖.不像有的验证码需要依赖canvas或者是需要编译,而且captchapng使用起来简单 ...

  3. svnserver权限问题

    打开visualSVN server 右键Users,新建user/Create user 输入username.password.确认password.依据须要建立对应的用户 右键Groups,新建 ...

  4. 【Android】15.2 广播

    分类:C#.Android.VS2015: 创建日期:2016-02-29 一.简介 Android系统和你自己编写的应用程序都可以通过Indent发送和接收广播信息.广播的内容既可以是自定义的信息, ...

  5. 使用【单独】的一个<script>进行js文件的引用

    刚才用jQuery的时候,总是发现js代码不被执行...后来发现我的代码是这么写的: <script type="text/javascript" src="htt ...

  6. LeetCode -- 推断链表中是否有环

    思路: 使用两个节点.slow和fast,分别行进1步和2步.假设有相交的情况,slow和fast必定相遇:假设没有相交的情况,那么slow或fast必定有一个为null 相遇时有两种可能:1. 仅仅 ...

  7. Android——Activity初学

    manifests里的AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> < ...

  8. Maven + Spring 进行多环境自动切换功能

    在pom.xml的<project></project>的最下放写入如下代码: <!-- profiles setting start [mvn install -P x ...

  9. Python之美[从菜鸟到高手]--NotImplemented小析

    今天写代码时无意碰到NotImplemented,我一愣.难道是NotImplementedError的胞弟,所以略微研究了一下. NotImplemented故名思议.就是"未实现&quo ...

  10. MySQL线程池总结

    线程池是Mysql5.6的一个核心功能,对于服务器应用而言,无论是web应用服务还是DB服务,高并发请求始终是一个绕不开的话题.当有大量请求并发访问时,一定伴随着资源的不断创建和释放,导致资源利用率低 ...