We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent df0f584 commit 32633ccCopy full SHA for 32633cc
validate-binary-search-tree/sooooo-an.ts
@@ -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