Skip to content

Commit c302622

Browse files
committed
solve(w02): 98. Validate Binary Search Tree
1 parent f5d85e4 commit c302622

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* 시간 복잡도: O(n)
3+
* 공간 복잡도; O(h)
4+
* -> h는 트리의 높이
5+
*/
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* function TreeNode(val, left, right) {
10+
* this.val = (val===undefined ? 0 : val)
11+
* this.left = (left===undefined ? null : left)
12+
* this.right = (right===undefined ? null : right)
13+
* }
14+
*/
15+
/**
16+
* @param {TreeNode} root
17+
* @return {boolean}
18+
*/
19+
var isValidBST = function(root) {
20+
function bst(node , min, max) {
21+
if (!node) return true;
22+
23+
if (node.val <= min || node.val >= max) return false;
24+
25+
return (
26+
bst(node.left, min, node.val) && helper(node.right, node.val, max)
27+
);
28+
}
29+
30+
return bst(root, -Infinity, Infinity);
31+
};

0 commit comments

Comments
 (0)