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.

For example:
Given the below binary tree,

       1
      / \
     2   3

 

Return 6.

 

Have you been asked this question in an interview? 

 

The problem can solved using the DFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int maxPathSum(TreeNode root) {
       int maxSum = Integer.MIN_VALUE; 
        if(root != null){
            Stack<TreeNode> sta = new Stack<TreeNode>();
            HashSet<TreeNode> hset = new HashSet<TreeNode>();
            sta.push(root);
            hset.add(root);
            while(!sta.empty()){
                TreeNode aNode = sta.pop();
                if(aNode.left != null && hset.add(aNode.left)){
                    sta.push(aNode);
                    sta.push(aNode.left);
                }
                else if(aNode.right != null && hset.add(aNode.right)){
                    sta.push(aNode);
                    sta.push(aNode.right);
                }
                else{
                    int leftMax = (aNode.left == null) ? 0 : aNode.left.val;
                    int rightMax = (aNode.right == null) ? 0 : aNode.right.val;
                    int sum = aNode.val + leftMax + rightMax;
                    aNode.val = Math.max(aNode.val, Math.max(leftMax + aNode.val, rightMax + aNode.val));
                    maxSum = Math.max(maxSum, Math.max(aNode.val, sum));                   
                }                          
            }      
        }
        return maxSum;
    }
}

 

Another solution: implementation of postorder tree traversal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class Solution {
    /**
     * This problem is a simple implementation of postorder tree traversal<br>
     *
     * @param root --root node of a tree
     * @return max path sum
     * @author Averill Zheng
     * @version 2014-06-02
     * @since JDK 1.7
     */
    public int maxPathSum(TreeNode root) {
        int maxSum = Integer.MIN_VALUE;
        Stack<TreeNode> node = new Stack<TreeNode>();
        if(root != null){          
            putNodeInStack(root, node);
            Map<TreeNode, Integer> maxAtNode = new HashMap<TreeNode, Integer>();
            TreeNode currentRightNode = null;
             
            while(!node.isEmpty()){
                TreeNode nodeInProcess = node.pop();
                if(nodeInProcess.right == null || currentRightNode == nodeInProcess.right){
                    int leftMax = 0 , rightMax = 0 ;
                    if(nodeInProcess.left != null)
                        leftMax = maxAtNode.get(nodeInProcess.left);
                    if(nodeInProcess.right != null)
                        rightMax = maxAtNode.get(nodeInProcess.right);
                    //take the max among leftMax + nodeInProcess.val, nodeInProcess.val, rightMax + nodeInProcess.val
                    //and rightMax + nodeInProcess.val + leftMax
                    //but in the Map, we can only save the max of first three
                     
                    maxSum = (maxSum < rightMax + nodeInProcess.val + leftMax) ? rightMax + nodeInProcess.val + leftMax : maxSum;
                    int maxOfNode = Math.max(Math.max(leftMax + nodeInProcess.val, nodeInProcess.val), rightMax + nodeInProcess.val);
                    maxSum = (maxSum < maxOfNode)? maxOfNode : maxSum;
                    maxAtNode.put(nodeInProcess, maxOfNode);
                     
                    if(!node.isEmpty() && node.peek().right == nodeInProcess)
                        currentRightNode = nodeInProcess;
                }
                else{
                    node.push(nodeInProcess);
                    putNodeInStack(nodeInProcess.right, node);
                }
            }
        }
        return maxSum;
    }
     
    private void putNodeInStack(TreeNode root, Stack<TreeNode> node){
        while(root != null){
            node.push(root);
            if(root.left != null){
                node.push(root.left);
                root = root.left;
            }
            else if(root.right != null){
                node.push(root.right);
                root = root.right;
            }
            else
                root = null;
        }
    }
}

  

  

posted @   Averill Zheng  阅读(142)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示