zoukankan      html  css  js  c++  java
  • 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;
    		}
    	}
    }
    

      

      

  • 相关阅读:
    2018年左其盛读过评过的书(持续更新中)
    2星|《用场景营销引爆你的生意》:总共4个推荐案例,3个已经失败
    2018左其盛经管新书差评榜(持续更新中)
    3星|《十大全球CEO亲授企业高速成长的关键战略》:作为CEO,我也非常坦率地表明过家庭优先于工作
    2018左其盛好书榜(持续更新中)
    3星|《你的品牌需要一个讲故事的人》:有理论没案例
    《思考快与慢》前传,两位天才犹太心理学家的传奇人生与学术故事:4星|《思维的发现》
    C#如何在派生类中不显示父类的一些属性以及TypeDescriptor使用
    在XML里的XSD和DTD以及standalone的使用
    数据库操作之简单带参操作
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3568343.html
Copyright © 2011-2022 走看看