forked from USPCodeLabSanca/dev.hire-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path98.cpp
More file actions
65 lines (52 loc) · 1.85 KB
/
98.cpp
File metadata and controls
65 lines (52 loc) · 1.85 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* 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) {
TreeNode* left = root->left;
TreeNode* right = root->right;
if (root->left != NULL) {
if (left->val >= root->val)
return false;
if (!this->checkBinaryTree(left, std::numeric_limits<long>::min(), root->val))
return false;
}
if (root->right != NULL) {
if (right->val <= root->val)
return false;
if (!this->checkBinaryTree(right, root->val, std::numeric_limits<long>::max()))
return false;
}
return true;
}
bool checkBinaryTree(TreeNode* node, long mi, long ma) {
if (node == NULL)
return true;
cout << node->val << "," << mi << "," << ma << endl;
if ((long)(node->val) > mi && (long)(node->val) < ma) {
TreeNode* left = node->left;
TreeNode* right = node->right;
if (left != NULL) {
if (this->checkBinaryTree(left, mi, min(ma, (long)(node->val))) == false)
return false;
}
if (right != NULL) {
if (right->val <= node->val)
return false;
if (this->checkBinaryTree(right, max(mi, (long)(node->val)), ma) == false)
return false;
}
return true;
}
return false;
}
};