My 2ms solution with a class


  • 0
    public class Solution {
    	    public int rob(TreeNode root) {
    	        if(root == null) return 0;
    	        House entrance = new House(root);
    	        return Math.max(entrance._norob, entrance._rob);
    	    }
    	    
    	    public class House{
    	    	public int _rob;
    	    	public int _norob;
    	    	TreeNode _root;
    	    	House left;
    	    	House right;
    	    	House(TreeNode root){
    	    		_root = root;
    	    		if(root.left != null) left = new House(root.left);
    	    		if(root.right != null) right = new House(root.right);
    	    		int leftNorob = left == null ? 0 : left._norob;
    	    		int leftRob = left == null ? 0 : left._rob;
    	    		int rightNorob = right == null ? 0 : right._norob;
    	    		int rightRob = right == null ? 0 : right._rob;
    	    		_rob = rightNorob + leftNorob + root.val;
    	    		_norob = Math.max(leftRob + rightRob, Math.max(rightNorob + leftNorob,Math.max(leftNorob + rightRob,leftRob + rightNorob)));
    	    	}
    	    }
    }
    

Log in to reply
 

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