-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay-089.cpp
25 lines (24 loc) · 898 Bytes
/
Day-089.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Definition for a binary tree node.
* 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 isValidBST(TreeNode* root, const std::optional<int64_t> &min_value=LLONG_MIN , const std::optional<int64_t>&max_value=LLONG_MAX) {
if(root == nullptr){
return true;
}
// according to DCP question binary search tree node can have equal value
if(root -> val > max_value.value() || root->val < min_value.value()){
return false;
}
return isValidBST(root->right,root->val,max_value) && isValidBST(root->left,min_value,root->val);
}
};