Skip to content

Commit ca2988b

Browse files
authored
Create 98-validate-binary-search-tree.js
1 parent fd97bed commit ca2988b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

98-validate-binary-search-tree.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {boolean}
11+
*/
12+
const isValidBST = function(root) {
13+
return helper(root, -Infinity, Infinity)
14+
}
15+
function helper(root, minValue, maxValue) {
16+
if (!root) return true
17+
if (root.val <= minValue || root.val >= maxValue) {
18+
return false
19+
}
20+
let leftSide = helper(root.left, minValue, root.val)
21+
let rightSide = helper(root.right, root.val, maxValue)
22+
return leftSide && rightSide
23+
}

0 commit comments

Comments
 (0)