Skip to content

Commit b80e22c

Browse files
committed
2주차 문제 풀이 1개 추가
- Validate Binary Search Tree
1 parent a8d75ac commit b80e22c

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
bool isValidBST(TreeNode* root) {
15+
vector<int> v;
16+
v.reserve(1e4);
17+
18+
solve(root, v);
19+
20+
int len = v.size();
21+
for (int i = 0; i < len - 1; ++i)
22+
{
23+
if (v[i] >= v[i + 1])
24+
{
25+
return false;
26+
}
27+
}
28+
29+
return true;
30+
}
31+
32+
void solve(TreeNode* root, vector<int>& v)
33+
{
34+
if (root->left != nullptr)
35+
{
36+
solve(root->left, v);
37+
}
38+
39+
v.push_back(root->val);
40+
41+
if (root->right != nullptr)
42+
{
43+
solve(root->right, v);
44+
}
45+
}
46+
};

0 commit comments

Comments
 (0)