-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBinaryTreeValidator.cpp
41 lines (37 loc) · 1.25 KB
/
BinaryTreeValidator.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
40
41
// Problem-Link: https://leetcode.com/problems/validate-binary-search-tree/
// 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 BinaryTreeValidator {
public:
bool isValidBST(TreeNode* root) {
if (root == nullptr) return true;
if (not isValidBST(root->left) || not isValidBST(root->right)) return false;
// Condition I: Max(root->left) < root->val
if (root->left) {
int max_left = getMax(root->left);
if (root->val <= max_left) return false;
}
// Condition II: Min(root->right) > root->val
if (root->right) {
int min_right = getMin(root->right);
if (root->val >= min_right) return false;
}
return true;
}
private:
int getMin(TreeNode* root) {
if (root->left != nullptr) return getMin(root->left);
return root->val;
}
int getMax(TreeNode* root) {
if (root->right != nullptr) return getMax(root->right);
return root->val;
}
};