Skip to content

Commit 85e759f

Browse files
committed
validate-binary-search-tree solution
1 parent 7b32f39 commit 85e759f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/* 시간 복잡도: O(N)
3+
* - 재귀 호출: 트리 노드의 개수(N) 만큼
4+
* 공간 복잡도: O(N)
5+
* - 재귀 호출: 트리 노드의 개수(N) 만큼
6+
*/
7+
public boolean isValidBST(TreeNode root) {
8+
return validate(root, Long.MIN_VALUE, Long.MAX_VALUE);
9+
}
10+
11+
boolean validate(TreeNode node, long min, long max) {
12+
if (node == null) return true;
13+
if (node.val <= min || node.val >= max) return false;
14+
return validate(node.left, min, node.val) && validate(node.right, node.val, max);
15+
}
16+
}
17+

0 commit comments

Comments
 (0)