forked from hijiangtao/LeetCode-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathres.js
27 lines (23 loc) · 721 Bytes
/
res.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function(root) {
if (!root) return true;
const validSubBST = (element, low, high) => {
if (low !== null && low >= element.val) return false;
if (high !== null && high <= element.val) return false;
if (!element.left && !element.right) return true;
if (element.left && !validSubBST(element.left, low, element.val)) return false;
if (element.right && !validSubBST(element.right, element.val, high)) return false;
return true;
}
return validSubBST(root, null, null);
};