-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path98_Validate_Binary_Search_Tree.cpp
More file actions
36 lines (31 loc) · 985 Bytes
/
Copy path98_Validate_Binary_Search_Tree.cpp
File metadata and controls
36 lines (31 loc) · 985 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// * 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 {
bool check(TreeNode *root, long long mn, long long mx)
{
if (root == NULL)
return true;
if ((long long)(root -> val) <= mn || (long long)(root -> val) >= mx)
return false;
return check(root -> left, mn, (long long)(root -> val)) && check(root ->right, (long long)root -> val, mx);
}
public:
bool isValidBST(TreeNode* root) {
if (root == NULL || (root -> left == NULL && root -> right == NULL))
return true;
return check(root, -1e18, 1e18);
}
};
int main() {
return 0;
}