Skip to content

Commit 32633cc

Browse files
committed
validate-binary-search-tree
1 parent df0f584 commit 32633cc

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function isValidBST(root: TreeNode | null): boolean {
2+
return validateBSTHelper(root, -Infinity, Infinity);
3+
}
4+
5+
const validateBSTHelper = (
6+
root: TreeNode | null,
7+
minValue: number,
8+
maxValue: number
9+
): boolean => {
10+
if (root === null) {
11+
return true;
12+
}
13+
if (root.val <= minValue || root.val >= maxValue) {
14+
return false;
15+
}
16+
const leftIsValid = validateBSTHelper(root.left, minValue, root.val);
17+
return leftIsValid && validateBSTHelper(root.right, root.val, maxValue);
18+
};

0 commit comments

Comments
 (0)