-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0098_Validate_Binary_Search_Tree.cpp
More file actions
34 lines (30 loc) · 1 KB
/
Copy path0098_Validate_Binary_Search_Tree.cpp
File metadata and controls
34 lines (30 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
bool rr(TreeNode* root, TreeNode* min_parent, TreeNode* max_parent){
if(root == NULL) return true;
if((min_parent != NULL) && (root->val <= min_parent->val)) return false;
if((max_parent != NULL) && (root->val >= max_parent->val)) return false;
return rr(root->left, min_parent, root) && rr(root->right, root, max_parent);
}
bool isValidBST(TreeNode* root) {
return rr(root, NULL, NULL);
}
};
int main(){
Solution solve;
TreeNode* t1 = new TreeNode(1);
TreeNode* t2 = new TreeNode(4);
TreeNode* t3 = new TreeNode(5, t1, t2);
std::cout << solve.isValidBST(t3) << std::endl;
return 0;
}