Skip to content

Commit 97c7c53

Browse files
committed
Added validateBinarySearchTree solution
1 parent 252ec00 commit 97c7c53

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var isValidBST = function (root) {
2+
return validate(root, -Infinity, Infinity);
3+
};
4+
5+
function validate(node, min, max) {
6+
if (!node) return true; // An empty tree is a valid BST
7+
if (node.val <= min || node.val >= max) return false; // Current node's value must be between min and max
8+
9+
// Recursively validate the left and right subtree
10+
return (
11+
validate(node.left, min, node.val) && validate(node.right, node.val, max)
12+
);
13+
}

0 commit comments

Comments
 (0)