Accepted recursive Javascript solution


  • 0
    R

    Open to comments on how to improve the solution.

    function getMinimumDifference(root, vals = { prev: undefined, min: Number.MAX_VALUE }) {
        if (!root) {
            return;
        }
    
        getMinimumDifference(root.left, vals);
    
        if (vals.prev >= 0) {
            vals.min = Math.min(Math.abs(vals.prev - root.val), vals.min);
        }
    
        vals.prev = root.val;
    
        getMinimumDifference(root.right, vals);
    
        return vals.min;
    }
    

Log in to reply
 

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