• 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;
    		}
    	}
    }
    

      

      

  • 相关阅读:
    [置顶] windows player,wzplayerV2 for windows
    wzplayer 近期将会支持BlackBerry和WinPhone8
    wzplayerEx for android(真正硬解接口,支持加密的 player)
    ffmpeg for ios 交叉编译 (支持i686 armv7 armv7s) 包含lame支持
    ffmpeg for ios 交叉编译 (支持i686 armv7 armv7s) 包含lame支持
    编译cegcc 0.59.1
    wzplayer 近期将会支持BlackBerry和WinPhone8
    wzplayerEx for android(真正硬解接口,支持加密的 player)
    windows player,wzplayerV2 for windows(20140416)更新
    编译cegcc 0.59.1
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3568343.html
Copyright © 2020-2023  润新知