-
Notifications
You must be signed in to change notification settings - Fork 3
/
98.cpp
24 lines (24 loc) · 833 Bytes
/
98.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 validate(TreeNode* node, long long minVal, long long maxVal) {
if (node == nullptr) return true;
bool current = (node->val > minVal && node->val < maxVal);
bool left = validate(node->left, minVal, node->val);
bool right = validate(node->right, node->val, maxVal);
return current && left && right;
}
bool isValidBST(TreeNode* root) {
return validate(root, LLONG_MIN, LLONG_MAX);
}
};