Skip to content

Commit 271d463

Browse files
author
이호찬
committed
validate-binary-search-tree solution
1 parent 3b32de0 commit 271d463

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
var isValidBST = function(root) {
14+
const dfs = (node, min, max) => {
15+
if (!node) return true;
16+
if (!((node.val > min) && (node.val < max))) return false;
17+
18+
return dfs(node.left, min, node.val) && dfs(node.right, node.val, max);
19+
}
20+
21+
return dfs(root, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
22+
};
23+
24+
// 시간 복잡도 O(n) , 공간 복잡도 최악의 경우 O(n)

0 commit comments

Comments
 (0)