Skip to content

Commit e9805b0

Browse files
committed
commit
1 parent 25b7e8f commit e9805b0

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed
Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,73 @@
1-
- https://leetcode.com/problems/validate-binary-search-tree/
1+
# 연관 링크
2+
- [문제 풀이 스케줄](https://github.com/orgs/DaleStudy/projects/6/views/5)
3+
- [답안 코드 제출법](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C)
4+
5+
# Problem
6+
- 문제 링크 : https://leetcode.com/problems/validate-binary-search-tree/
7+
- 문제 이름 : validate-binary-search-tree
8+
- 문제 번호 : 251
9+
- 난이도 : medium
10+
- 카테고리 : binary search tree
11+
12+
# 문제 설명
13+
14+
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
15+
16+
A valid BST is defined as follows:
17+
18+
The left subtree of a node contains only nodes with keys less than the node's key.
19+
The right subtree of a node contains only nodes with keys greater than the node's key.
20+
Both the left and right subtrees must also be binary search trees.
21+
22+
#### Constraints:
23+
24+
- The number of nodes in the tree is in the range [1, 104].
25+
- 2^31 <= Node.val <= 2^31 - 1
26+
27+
# 아이디어
28+
- 각 노드 별
29+
30+
31+
32+
# 코드 (Solution)
33+
```cpp
34+
35+
class Solution {
36+
public:
37+
bool isValidBSTHelper(TreeNode* root, long min, long max){
38+
if (!root) {
39+
return true;
40+
}
41+
42+
if (root->val <= min || root->val >= max) {
43+
return false;
44+
}
45+
46+
return isValidBSTHelper(root->left, min, root->val) &&
47+
isValidBSTHelper(root->right, root->val, max);
48+
}
49+
50+
bool isValidBST(TreeNode* root) {
51+
return isValidBSTHelper(root, LONG_MIN, LONG_MAX);
52+
}
53+
};
54+
```
55+
56+
## 코드 설명
57+
58+
- INT_MIN / INT_MAX가 Constraint의 경계값이지만, 해당 값을 이용할 경우 경계값 비교 시 번거로워지므로 LONG으로 비교하도록 처리
59+
60+
61+
# 최적화 포인트 (Optimality Discussion)
62+
- 특이 사항 없음
63+
64+
# 🧪 테스트 & 엣지 케이스
65+
66+
## INT_MAX / INT_MIN으로 할 경우
67+
- root = [2147483647] 인 경우 엣지 케이스
68+
69+
# 📚 관련 지식 복습
70+
71+
# 🔁 회고
72+
73+

0 commit comments

Comments
 (0)