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:

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10  
11 class Result{
12     int singlePath;
13     int endPath;
14     
15     public Result(){
16         singlePath = 0;
17         endPath = Integer.MIN_VALUE;
18     }
19     
20     public Result(int s, int e){
21         singlePath = s;
22         endPath = e;
23     }
24 }
25  
26 public class Solution {
27     public int maxPathSum(TreeNode root) {
28         Result res = maxPathSumRecur(root);
29         return res.endPath;
30     
31         
32     }
33     
34     
35     public Result maxPathSumRecur(TreeNode cur){
36         if (cur==null){
37             Result res = new Result();
38             return res;
39         }
40         
41         Result left = maxPathSumRecur(cur.left);
42         Result right = maxPathSumRecur(cur.right);
43         Result res = new Result();
44         
45         res.singlePath = Math.max(left.singlePath, right.singlePath);
46         res.singlePath = Math.max(res.singlePath,0);
47         res.singlePath += cur.val;        
48         
49         res.endPath = Math.max(left.endPath, right.endPath);
50         int temp = cur.val;
51         if (left.singlePath>0) temp+=left.singlePath;
52         if (right.singlePath>0) temp+=right.singlePath;
53         res.endPath = Math.max(res.endPath, temp);
54         
55         return res;
56     }
57             
58             
59 }

 

 

 

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.

 

posted @ 2014-11-08 07:54  LiBlog  阅读(219)  评论(0编辑  收藏  举报