File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
validate-binary-search-tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments