-
Notifications
You must be signed in to change notification settings - Fork 5
/
Solution.cpp
40 lines (35 loc) · 939 Bytes
/
Solution.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
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tuple>
#include <deque>
#include <unordered_map>
#include <cmath>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool help(TreeNode* root, int mi, int ma, bool llimit, bool rlimit) {
if (root == NULL) return true;
if (llimit and root->val <= mi or rlimit and root->val >= ma) return false;
return help(root->left, mi, min(ma, root->val), llimit, true)
and help(root->right, max(mi, root->val), ma, true, rlimit);
}
bool isValidBST(TreeNode* root) {
int ma = ((unsigned int) ~0) >> 1;
int mi = 1 << 31;
return help(root, mi, ma, false, false);
}
};
int main() {
Solution s;
s.isValidBST(NULL);
return 0;
}