Very easy to understand java solution


  • 0
    A
    	private int max(TreeNode root) {
    		if (root.right != null) {
    			return max(root.right);
    		}
    		else {
    			return root.val;
    		}
    	}
    
    	private int min(TreeNode root) {
    		if (root.left != null) {
    			return min(root.left);
    		}
    		else {
    			return root.val;
    		}
    	}
    
    	public int getMinimumDifference(TreeNode root) {
        	if (root == null) {
        		return Integer.MAX_VALUE;
        	}
        	int minLeft = getMinimumDifference(root.left);
        	int minRight = getMinimumDifference(root.right);
        	if (root.left != null) {
        		int max = max(root.left);
        		int diff = Math.abs(max - root.val); 
        		minLeft = Math.min(diff, minLeft);
        	}
        	if (root.right != null) {
        		int min = min(root.right);
        		int diff = Math.abs(min - root.val);
        		minRight = Math.min(minRight, diff);
        	}
        	return Math.min(minLeft, minRight);
        }
    

Log in to reply
 

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