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. JEECG技术总结

    1.用户数据迁移时,必须在表t_s_base_user和t_s_user中都插入数据才会在页面显示.2.执行sql语句: String sql = "select count(id) fro ...

  2. mysql group replication 安装&配置详解

    一.原起: 之前也有写过mysql-group-replication (mgr) 相关的文章.那时也没有什么特别的动力要写好它.主要是因为在 mysql-5.7.20 之前的版本的mgr都有着各种各 ...

  3. SQLSERVER中的timestamp 和 C#中的byte[] 转换

    项目中由于需求设计,数据库中需要一个timestamp时间戳类型的字段来作为区别数据添加和修改的标识.由于timestamp在SQL SERVER 2005数据库中,不可为空的timestamp类型在 ...

  4. 蜜果私塾:informix数据库学习合集[不断补充]

    一.infomix使用备忘录     目录结构:     1. 启动与停止命令:      2. 修改数据库编码:      3. 查看informix占用的端口:      4. 使用dbacces ...

  5. js基本知识5

    1.1 复习 1. 按钮不可用 disabled = “disabled” || true 2. setTimeout 只执行一次 setInterval 执行很多次 3. 递归调用 : 函数自己调用 ...

  6. Ubuntu下安装RabbbitVCS(图形化svn管理工具)- Ubuntu也有TortoiseSVN

    在Windows下用惯了TortoiseSVN这只小乌龟,到了Ubuntu下很不习惯命令行的SVN,于是经过一番寻找安装了RabbitVCS这款SVN图形化前端工具(官方网站:http://rabbi ...

  7. Ununtu 15.04 安装MySql(Django连接Mysql)

    本文介绍Ubuntu 15.04下安装MySQL ubuntu 15.04安装mysql django项目连接mysql 一.安装数据库 1.sudo apt-get install mysql-se ...

  8. linux - fpga-framebuff驱动

    * linux/drivers/video/fpga_fb.c --fpga graphics adaptor frame buffer device *  Created 16 Sep2011 *  ...

  9. Office Web App2013 在线查看PDF文件

    经常会有客户问,在SharePoint中,如何在浏览器中查看与编辑文档,通常给出的解决方案是集成Office Web App. 而在实际应用过程中,客户通常会要求实现PDF文件在线查看,对于PDF文件 ...

  10. ardunio

    fritzing,  arduino简易电路图制作软件