-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path98.cpp
26 lines (26 loc) · 850 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
25
26
/**
* 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 isWithinLimits(TreeNode *root, long long min, long long max) {
if (!root)
return true;
bool currentValid = root->val < max && root->val > min;
return currentValid && isWithinLimits(root->left, min, root->val) &&
isWithinLimits(root->right, root->val, max);
}
bool isValidBST(TreeNode *root) {
return isWithinLimits(root->left, LLONG_MIN, root->val) &&
isWithinLimits(root->right, root->val, LLONG_MAX);
}
};