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
+ * 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 ) : boolean {
16
+ if ( ! root ) return true ;
17
+
18
+ function isValid ( node : TreeNode | null , min : number , max : number ) : boolean {
19
+ if ( ! node ) return true ;
20
+ if ( node . val <= min || node . val >= max ) return false ;
21
+
22
+ return isValid ( node . left , min , node . val ) &&
23
+ isValid ( node . right , node . val , max )
24
+ }
25
+
26
+ // ์ด๊ธฐ ํธ์ถ (๋ฃจํธ ๋
ธ๋์ ๋ฒ์๋ ๋ฌดํ๋)
27
+ return isValid ( root , - Infinity , Infinity )
28
+ }
You canโt perform that action at this time.
0 commit comments