首页 技术 正文
技术 2022年11月21日
0 收藏 449 点赞 3,505 浏览 3886 个字

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.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,356
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,139
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848