File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
validate-binary-search-tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
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 isValidBST ( root , low = - Infinity , high = Infinity ) {
14
+ if ( root === null ) {
15
+ return true ;
16
+ }
17
+
18
+ if ( root . val <= low || root . val >= high ) {
19
+ return false ;
20
+ }
21
+
22
+ return (
23
+ // left root should be less than current root
24
+ isValidBST ( root . left , low , root . val ) &&
25
+ // right root should be greater than current root
26
+ isValidBST ( root . right , root . val , high )
27
+ ) ;
28
+ }
You can’t perform that action at this time.
0 commit comments