-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay25.cpp
62 lines (52 loc) · 1.46 KB
/
Day25.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <limits.h>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
};
Node* createNode(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->left = nullptr;
newNode->right = nullptr;
return newNode;
}
Node* insertLevelOrder(int arr[], Node* root, int i, int n) {
if (i < n && arr[i] != -1) {
Node* temp = createNode(arr[i]);
root = temp;
root->left = insertLevelOrder(arr, root->left, 2 * i + 1, n);
root->right = insertLevelOrder(arr, root->right, 2 * i + 2, n);
}
return root;
}
bool isValidBST(Node* root, long long minValue, long long maxValue) {
if (root == nullptr) {
return true;
}
if (root->data <= minValue || root->data >= maxValue) {
return false;
}
return isValidBST(root->left, minValue, root->data) &&
isValidBST(root->right, root->data, maxValue);
}
int main() {
int n;
cout << "Enter the number of nodes in the tree: ";
cin >> n;
int arr[n];
cout << "Enter the tree nodes in level order (use -1 for null): ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
Node* root = nullptr;
root = insertLevelOrder(arr, root, 0, n);
if (isValidBST(root, LLONG_MIN, LLONG_MAX)) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}