Skip to content

Latest commit

 

History

History
25 lines (22 loc) · 915 Bytes

Validate Binary Search Tree.md

File metadata and controls

25 lines (22 loc) · 915 Bytes

Screen Shot 2022-02-04 at 00 06 26

Screen Shot 2022-02-04 at 00 06 33

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isValidBST = function(root, min = null, max = null) {
    if(!root) return true;
    if(min && root.val <= min.val) return false;
    if(max && root.val >= max.val) return false;
    return isValidBST(root.left, min, root) && isValidBST(root.right, root, max);
};