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

      

      

  • 相关阅读:
    关于JSON可能出现的错误,待更/todo
    mongoose的安装与使用(书签记录) 2017
    HTTP的学习记录3--HTTPS和HTTP
    HTTP的学习记录(二)头部
    HTTP(一)概述
    LeetCode 455. Assign Cookies
    LeetCode 453. Minimum Moves to Equal Array Elements
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 447. Number of Boomerangs
    LeetCode 416. Partition Equal Subset Sum
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3568343.html
Copyright © 2011-2022 走看看