Runtime Error here, but fine in my own eclipse, same code


  • 0
    F
    public class Solution {
        public int maxPathSum(TreeNode root) {
            HashMap<TreeNode,Integer> node_notRootMax = new HashMap<TreeNode,Integer>();
    		HashMap<TreeNode,Integer> node_asRootMax = new HashMap<TreeNode,Integer>();
    		
            int maxPathNotRoot ;
    		int maxPathAsRoot;
    		int curMax;
    		int maxPathFinal = -10000;
    		TreeNode p = root;
    		
    		if(p == null)
    		    return 0;
    		    
    		//max for left tree
    		while ((p = p.left)!=null) {
    			maxPathNotRoot = maxPathNotRoot(p,node_notRootMax);
    			maxPathAsRoot = maxPathAsRoot(p,node_notRootMax,node_asRootMax);
    			
    			curMax = maxPathNotRoot > maxPathAsRoot ? maxPathNotRoot:maxPathAsRoot;
    			if(curMax > maxPathFinal) 
    				maxPathFinal = curMax;
    		}
    		
    		p = root;
    		//max for right tree
    		while((p = p.right)!=null){
    			maxPathNotRoot = maxPathNotRoot(p,node_notRootMax);
    			maxPathAsRoot = maxPathAsRoot(p,node_notRootMax,node_asRootMax);
    			
    			curMax = maxPathNotRoot > maxPathAsRoot ? maxPathNotRoot:maxPathAsRoot;
    			if(curMax > maxPathFinal) 
    				maxPathFinal = curMax;
    		}
    	
    		//max for root
    		maxPathNotRoot = maxPathNotRoot(root,node_notRootMax);
    		maxPathAsRoot = maxPathAsRoot(root,node_notRootMax,node_asRootMax);
    		
    		curMax = maxPathNotRoot > maxPathAsRoot ? maxPathNotRoot:maxPathAsRoot;
    		if(curMax > maxPathFinal) {
    			maxPathFinal = curMax;
    		}
    		
    		return maxPathFinal;
        }
        
        public int maxPathNotRoot(TreeNode curRoot,HashMap<TreeNode, Integer> node_notRootMax) { 
            if (node_notRootMax.containsKey(curRoot)) 
    			return node_notRootMax.get(curRoot);
    		
    		if(curRoot.left == null && curRoot.right == null){
    		    node_notRootMax.put(curRoot, curRoot.val);
    		    return curRoot.val;
    		}
    			
    		int leftMax = maxPathNotRoot(curRoot.left,node_notRootMax);
    		int rightMax = maxPathNotRoot(curRoot.right,node_notRootMax);
    		
    		int curMax = (leftMax >= rightMax ? (leftMax+curRoot.val): (rightMax + curRoot.val)) ;
    		node_notRootMax.put(curRoot, curMax);
    		return curMax;
    	}
    	
    	//return the max sum of curRoot as root in the final path (meaning curRoot is in the path, parent is not, taking both left and right in the path)
    	public int maxPathAsRoot(TreeNode curRoot,HashMap<TreeNode, Integer> node_notRootMax, HashMap<TreeNode, Integer> node_asRootMax){
    	    if (node_asRootMax.containsKey(curRoot))
    			return node_asRootMax.get(curRoot);
    
            int curMax = -10000;
            
    		if (curRoot.left == null && curRoot.right == null) {
    			curMax = curRoot.val;
    		}	
    		else if (curRoot.left == null && curRoot.right != null) {
    			curMax =  maxPathNotRoot(curRoot.right,node_notRootMax) +curRoot.val;
    		} 
    		else if (curRoot.left != null && curRoot.right == null) {
    			curMax =  maxPathNotRoot(curRoot.left,node_notRootMax) +curRoot.val;
    		} 
    		else {
    			curMax = maxPathNotRoot(curRoot.left,node_notRootMax)
    				+ maxPathNotRoot(curRoot.right,node_notRootMax) + curRoot.val;
    		}
    		
    		node_asRootMax.put(curRoot, curMax);	
    		return curMax;
    	}	
    }
    

    The runtime error I got is said to from running input {1,2}.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.