Skip to content

Commit f9cc5ca

Browse files
authored
validate binary search tree solution
1 parent 548eca8 commit f9cc5ca

File tree

1 file changed

+19
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)