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

/**
 * 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

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 @ 2014-02-26 09:22  Averill Zheng  阅读(142)  评论(0编辑  收藏  举报