Skip to content

Commit a072f43

Browse files
committed
validate-binary-serach-tree
1 parent f121702 commit a072f43

File tree

1 file changed

+24
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)