Ryan76's recursive Java solution in Javascript (10 lines)


  • 0
    E
    var isValidBSTHelper = function(root,inheritedMin,inheritedMax) {
        if (!root) { return true; }
        if (inheritedMin!==null&&root.val<=inheritedMin) { return false; }
        if (inheritedMax!==null&&root.val>=inheritedMax) { return false; }
        return isValidBSTHelper(root.left,inheritedMin,root.val)
                &&isValidBSTHelper(root.right,root.val,inheritedMax);
    };
    var isValidBST = function(root) {
        return isValidBSTHelper(root,null,null);
    };

Log in to reply
 

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