Java solution - concise


  • 0
    S
    class Solution {
        int min = Integer.MAX_VALUE;
        TreeNode lastNode = null;
        
        public int getMinimumDifference(TreeNode root) {
            if(root == null) return min;
            min = getMinimumDifference(root.left);
            if(lastNode != null){
                min = Math.min(min, Math.abs(root.val - lastNode.val));
            }
            lastNode = root;
            return getMinimumDifference(root.right);
        }
    }
    

Log in to reply
 

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